How to break FOR-loop if "progressbar" is closed

1 view (last 30 days)
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!

Answers (1)

Robert Cumming
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
Nicholai
Nicholai on 19 Apr 2011
The problem is a close function doesn't work, since I need it to stop some calculations in another m-file. I got "exit" to work, but since this statement closes the whole Matlab application, I need something that just terminates all calculations in all m-files in my workspace... Thanks
Robert Cumming
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!