waitbar cancel button does nothing!

2 views (last 30 days)
Rebecca Vos
Rebecca Vos on 4 Mar 2015
Commented: Sean de Wolski on 5 Mar 2015
So I have this bit of code where I make a waitbar at the start with a cancel button. There is a bit to stop it if the waitbar is pushed, but if I run it it doesn't work - the button is pushed but it doesn't stop the calculation. What am I doing wrong?? Is it something to do with the waitbar handle h? Before I had it as handles.waitbar, but the same thing happened! Please help me!
while get(t,'UserData') tic; %Start a stopwatch timer. TIC and TOC functions work together to measure elapsed time. h = waitbar((time /handles.PARAMS.time_max), sprintf('%3.2f', (time /handles.PARAMS.time_max * 100) ),'CreateCancelBtn','setappdata(gcbf,''canceling'',1)'); setappdata(h,'canceling',0);
% does some stuff
if getappdata(h,'canceling') % Cancel button has been pressed
time = time + toc; % updating time duration of the calibration
Statut = true; % Indicating how ends the calibration
delete(h);
break
end
% do other calculations, not using waitbar
time = time + toc; % updating time duration of the calibration
pause(0.01); % In order to escape the loop if we press the "cancel button"
delete(h);
end
  1 Comment
Rebecca Vos
Rebecca Vos on 4 Mar 2015
but when I put "setappdata(h,'canceling');" before the loop it cancels it fine... so don't know what is happening here

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 4 Mar 2015
gcbf
Is very dangerous and should be avoided all of the time. It could be a figure, it could be a button, axes etc, who knows! In general for an applicatiom, all of the g* functions should be avoided (gcf, gca, gco etc.) because they're dynamic.
Instead, hardwire the handle to the object whose appdata is being referenced:
fig = figure
setappdata(fig,'cancelling',false)
etc.
  2 Comments
Rebecca Vos
Rebecca Vos on 4 Mar 2015
but it's not a figure, if I do that it just creates a blank figure? Sorry I'm not very good at this...
Sean de Wolski
Sean de Wolski on 5 Mar 2015
This line probably does nothing
setappdata(gcbf,''canceling'',1);
This one does because h doesn't change
setappdata(h,'canceling',0);
use a variable for whatever you want gcbf to be referencing in the first setappdata and use it (probably h again?).

Sign in to comment.

Categories

Find more on Dialog Boxes 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!