MATLAB Answers

Callback of uicontrol fails to execute

5 views (last 30 days)
abc
abc on 9 Nov 2013
Commented: abc on 10 Nov 2013
Hi,
I have an application for which I developed a progressbar figure with the help of a function I wrote progressbar_auto. I wanted a cancel button on the progressbar which I created using uicontrol. The callback function is executed ONLY when I put a breakpoint after the uicontrol statement and then click on the button Cancel that I created. I thought MATLAB would by default keep checking the push button to see if it has been clicked. I think the issue is that MATLAB does not check for the button until it has completed the remaining processes, and in this case the entire process may even take an hour which is why I kept a progressbar. I even tried creating the uicontrol push button in my main code instead of the function progressbar_auto, that still does not work. It seems that MATLAB recognizes the clicking of the button only if I click on Cancel right after it created a uicontrol for which I put a breakpoint, this is obviously not the way it should work. What should I do? Can anyone give me some advice on how to include a responsive cancel button on the progressbar for a long process?

  0 Comments

Sign in to comment.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 9 Nov 2013
Hi,
You are right, if you have some lengthy code/calculation, Matlab queues the execution of the callback. Add to your code (e.g. in a loop at the end or anywhere else where it makes sense a call to drawnow. This will make matlab process the event queue and your callback should execute.
Titus

  3 Comments

abc
abc on 9 Nov 2013
Hi, Thank you for your answer. I think it will work, but let me just verify that I am understanding it correctly. You are suggesting that I put a for loop like this somewhere in my code?
for i = 1 :500000 hbutton=uicontrol(progfig,'style','pushbutton','string','Cancel','Position', [550 0 100 20],'Callback',@Button_Callback); drawnow; end
When this loop is being executed, all other tasks will be suspended right? That would slow down the process tremendously. Please correct me if I am wrong. Thank you very much for your help.
Walter Roberson
Walter Roberson on 9 Nov 2013
You currently have an existing loop....
for variable = whatever
get value from control
if value indicates cancel, then break, end
...
end
as your main controlling code.
Change that to
for variable = whatever
drawnow
get value from control
if value indicates cancel, then break, end
...
end
abc
abc on 10 Nov 2013
I currently do not have a for loop to check the cancel button. I just have this :
hbutton=uicontrol('style','pushbutton','string','Cancel','Position', [550 0 100 20],'Callback',@Button_Callback);
Wont a for loop slow down the application? If it is necessary, how many iterations should it run. Also should it be within the function written for progressbar or in the main function which calls progressbar?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!