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

조회 수: 2 (최근 30일)
Nicholai
Nicholai 2011년 4월 18일
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