Different ways to execute a function
gravityhomer
I know that you can execute a function from the prompt, or by some event like a mouse click, or the moment that an experiment opens the first time. Maybe there is some global variable that is changed any time igor is active or not? and I can trigger off of that?
I have a program that communicates with a scale for weighing things, and any time the Igor window becomes active, I'd like it to automatically go get the weight. Right now I have it executing from a button click, and I'd like to remove that step.
I have been using igor for last couple of years, but I'm still pretty new to it.
I appreciate any help.
Thanks!
GH
You could use a hook function that triggers on the 'activate' event for the window. To set the hook function you use the SetWindow operation as in:
SetWindow myPanelName hook=myHookFunction
You need to have a (compiled) hook function in your procedure that looks something like:
Function myHookFunction(infoStr)
String infoStr
String event= StringByKey("EVENT",infoStr)
strswitch(event)
case "activate":
runTheScale()
break
endswitch
return 0
End
There are other options using named hooks but this approach should be simple enough.
HTH,
A.G.
WaveMetrics, Inc.
September 29, 2008 at 09:58 am - Permalink
Thanks for the quick reply, Igor. I will try a hook function, it seems that should do it. What is the above line though, I'm getting an error on that one?
September 29, 2008 at 02:02 pm - Permalink
That would be whatever the name of the function you've written that actually collects the measurement from your scale.
September 29, 2008 at 02:50 pm - Permalink
haha, woops, thanks. Brain wasn't working on that one.
September 30, 2008 at 05:09 am - Permalink
NewPanel /N=myPanelName
SetWindow myPanelName hook=myHookFunction
end
Function myHookFunction(infoStr)
String infoStr
String event= StringByKey("EVENT",infoStr)
strswitch(event)
case "activate":
Test1()
break
endswitch
return 0
End
Function Test1()
Variable/G n
n+=1
Print "hello "+num2str(n) +" times"
end
September 30, 2008 at 05:39 am - Permalink
The hook function is called with "activate" message whenever the window is activated. It does not differentiate between cases where the previously active window belonged to IGOR or to another application. If you plan to work on multiple windows within IGOR the hook function approach is not ideal. I recommend that you simply add a button control somewhere and press the button when you are sure that you want a measurement.
AG
September 30, 2008 at 09:42 am - Permalink
Oh, it seems to be working fine now for external programs now too. The problem seemed to come before if I used the Start bar to switch back and forth between active programs.
But I agree, this is not the ideal way to trigger something. It is just something I am trying as a simple work around.
Thanks for the help. This forum is awesome. I will definitely check back often.
October 1, 2008 at 05:53 am - Permalink