Give priority to gui callback over code

Hi,
I'm having some issues right now that I would need to be sorted. Here is the situation :
I need to monitor some data, and I want to do it through an interface. (I used the app designer functions)
I have a main program, and an interface which should display some data from the main running program.
The function to generate the interface is called from the main program so that it can have access to the handles.
In order to disturb the main program as little as possible, I put a "Refresh" button on the interface. The "refresh" callback function is nested in the main program. This function updates all the fields of the interface so that it shows the updated data from the main program.
Since I'm starting with some tests, I did this at first :
tic;
while toc < 30
value = toc;
pause(1);
end
With this first test, the refresh button works, my value is displayed in the interface as expected.
But then, I did this :
tic;
while toc < 30
value = toc;
end
Now, clicking on the button will not trigger any change in the interface. It seems that the callback nevers runs until I terminate the program.
Is there any way I can get that fixed ? I cannot afford to leave this "pause" function since the actual main program is supposed to monitor animal behaviour...

 Accepted Answer

Hi,
the pause allows interruption of the code, so yes, it is needed. You can
  • use a much smaller time like pause(0.001)
  • or use drawnow instead of pause. Drawnow allows interruption but doesn't wait. It's like pause with the smallest waiting value possible
Titus

5 Comments

BTW, you need to explain a little more, why you cannot leave the "pause" in there. And second, you might also use the timer function to let MATLAB do something in 30s from now.
Drawnow seems to be working fine. I tried to roughly measure how long it takes to run this function, and on my computer it's ~1ms, which is ok here. Also I thought the smallest time for the "pause" function was 10ms, which is too long for me.
I'm supposed to monitor mice behaviour with this whole thing (we use automated behavioural cages), so I can't afford the 10ms or I might miss something...
And the 30 seconds are actually meaningless and I only use them for this tests. In the actual program, I use a huge loop which only ends when all my mice have done what they're supposed to.
You might tell me that Matlab is not the best language to do such things and I totally agree with you, but this is not up to me.
Anyway, thanks for your quick answer and for solving my problem !
Little update, I just tried to use "pause(0)" as a total shot in the dark, and if I trust the tic/toc functions, it lasts 0.000006 on my setup, which is smaller than the drawnow.
As a staff member, I thought you could be interested.
Thanks again, have a nice day !
Hi Guillaume, your welcome. Yes, pause(0) has the same effect with the advantage of not watching out for redrawing of any graphics. Therefore it could be indeed faster, thanks for the reminder :)
I do not seem to see any documentation that pause(0) treats the event queue or graphics any differently;
https://www.mathworks.com/help/matlab/ref/pause.html#butxu5w-2 says that graphics updates and callbacks keep running
"When an object's Interruptible property is set to 'on', its callback can be interrupted at the next occurrence of one of these commands: drawnow, figure, getframe, waitfor, or pause.
If the running callback contains one of these commands, then MATLAB stops the execution of the running callback and executes the interrupting callback. MATLAB resumes executing the running callback when the interrupting callback completes."
which says nothing about pause(0) being special.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!