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

2 次查看(过去 30 天)
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!

回答(1 个)

Robert Cumming
Robert Cumming 2011-4-18
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 个评论
Nicholai
Nicholai 2011-4-19
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 2011-4-19
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by