Is there a function in Matlab to exit the whole session run without exit Matlab? I mean an equivalent to "goto end" function.

댓글 수: 4

Preethi
Preethi 2016년 2월 20일
you want to close all the windows and clear data without closing matlab? or you want to exit from matlab?
nilsotto
nilsotto 2016년 2월 20일
For instance if I have a conditional if loop and I do not just want to leave the loop, I want to go to the end of the program (without closing down Matlab)
Walter Roberson
Walter Roberson 2016년 2월 20일
error() out, provided nothing happens to be using try/catch .
Stephen23
Stephen23 2016년 2월 21일
return

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

답변 (2개)

Walter Roberson
Walter Roberson 2016년 2월 20일

0 개 추천

In any one loop, you can use "break" to terminate the loop.
In any one function, you can "return" to continue with the calling function. This may cause problems if you have not defined the return values that the calling function is expecting.
You can use error() to quit back through multiple levels until eventually the code reaches a place that is using try/catch for error control. There is no "uncatchable error".
You just might be able to use Java Robot Class to send your MATLAB session a control-C; I am not sure that will work, but it might. control-C cannot be caught, but it can also leave the program in a strange state.
You can exit out of MATLAB completely with "exit" or "quit" (which are equivalent.)
Jan
Jan 2016년 2월 20일
편집: Jan 2016년 2월 20일

0 개 추천

You can enclose you code in try catch block and use the MException identifier to recognize a regular abortion:
function Main
try
yourFunction;
catch ME
if strcmpi(ME.identifier, 'FlowControl:regularExit')
disp('Regular exit.')
else
rethrow(ME); % Or: throwAsCaller(ME)
end
end
end
function yourFunction
if rand > 0.5
ME = MException('FlowControl:regularExit', ...
'%s: Aborted due to random reason', mfilename);
throw(ME); % Goodbye
end
disp('Function not aborted.')
end
You see, this is not trivial or nice. But it allows a proper cleanup and error handling.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2016년 2월 20일

댓글:

2016년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by