How to break FOR-loop if "progressbar" is closed
2 views (last 30 days)
Show older comments
I have an m-file which makes a lot of Matrix calculation and in another m-file I have implemented a progressbar, to keep track of the calculations. I have a function so if the progressbar is closed the calculations stops:
function closeBar(src,evnt)
|selection = questdlg('Do you want to stop this process?',...
'Stop process',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
How do I break the FOR-loop in the other m-file, if case "Yes" is fulfilled?
Many thanks!
0 Comments
Answers (1)
Robert Cumming
on 18 Apr 2011
Below is an example of how to do it using two functions and a dialog which is created where you can interrupt - I'm sure you could take this as a skeleton and modify to suit.
EDIT:
function FUNCTION_IN_FILE1
h = dialog ( 'WindowStyle', 'Normal', 'position', [400 400 100 100], 'visible', 'off' );
FILE2_StartGuiForInterruption ( h );
while true
pause ( 0.1 );
disp ( 'in loop doing calculations' );
if ishandle ( h ) == false
break
end
end
disp ( 'finished' );
end
function FILE2_StartGuiForInterruption ( parent )
uicontrol ( 'parent', parent, 'style', 'pushbutton', 'Callback', {@FILE2_STOP}, 'position', [20 20 60 60], 'string', 'stop', 'backgroundcolor', 'red' );
set ( parent, 'visible', 'on' );
end
function FILE2_STOP ( obj, event )
close ( get ( obj, 'parent' ) );
end
2 Comments
Robert Cumming
on 19 Apr 2011
Why does close not work?
I've updated the function above - It can work in the say I stated, and I cant see why you state close doesn't work.
The thing I've shown you is a way to have an m file stop by pressing a button which comes from another m file.
The trick is to have the m file you want to stop know the handle to the GUI - that way it can check if the GUI has been closed.
See Also
Categories
Find more on Migrate GUIDE Apps 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!