waitbar cancel button does nothing!

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

Rebecca Vos
Rebecca Vos 2015년 3월 4일
but when I put "setappdata(h,'canceling');" before the loop it cancels it fine... so don't know what is happening here

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
Sean de Wolski 2015년 3월 4일

1 개 추천

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

Rebecca Vos
Rebecca Vos 2015년 3월 4일
but it's not a figure, if I do that it just creates a blank figure? Sorry I'm not very good at this...
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?).

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2015년 3월 4일

댓글:

2015년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by