return doesn't work within function
26 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I've created a function that should cancel the base script. I call the function with the following code:
if isempty(factor.editingImages.fillHoles) == true
scriptFailed('Failure during selection of fill-threshold.');
end
And this is the function:
function [] = scriptFailed(failureText)
%scriptFailed saves the current workspace and displays a message
% The message can be defined by failureText (string-variable)
filename = [datestr(now,'yyyy-mm-dd,HH.MM.SS'),' canceledWorkspace.mat'];
evalin('base',sprintf('save(''%s'')',fullfile(pwd,filename)))
disp([failureText,' The programm canceled and saved the workspace ("',filename,'").'])
close all
return
end
But after calling the function (workspace is always created as it should be, so the function is always active), the next lines after the if-loop are trying to do something but can't, because there are some variables missing. Why does return doesn't cancel the running script?
Thanks for your help!
0 件のコメント
採用された回答
Matt J
2020 年 11 月 10 日
編集済み: Matt J
2020 年 11 月 10 日
You cannot give a function the power to abort another function. See also,
Your code should look like this, instead:
if isempty(factor.editingImages.fillHoles) == true
scriptFailed('Failure during selection of fill-threshold.');
return
end
function [] = scriptFailed(failureText)
%scriptFailed saves the current workspace and displays a message
% The message can be defined by failureText (string-variable)
filename = [datestr(now,'yyyy-mm-dd,HH.MM.SS'),' canceledWorkspace.mat'];
evalin('base',sprintf('save(''%s'')',fullfile(pwd,filename)))
disp([failureText,' The programm canceled and saved the workspace ("',filename,'").'])
close all
end
6 件のコメント
Matt J
2020 年 11 月 10 日
No, see my edited answer. You can abort the current script as long as you call return in the workspace of that script.
その他の回答 (1 件)
Steven Lord
2020 年 11 月 10 日
Why does return doesn't cancel the running script?
It does exit from the currently running function. It exits the scriptFailed function, returning control to the script or function that called scriptFailed.
To do what you want, either:
- move the return keyword from inside scriptFailed to just after scriptFailed is called in its caller. Note that if scriptFailed itself was called from a function, it will return back to control of its caller.
- Have scriptFailed throw an error indicating the problem. Unless something higher up in the caller stack is running this code in the try section of a try / catch block the error will work its way up through the caller stack and eventually cause the top-level script or function to exit. Even if something higher in the caller stack does try / catch the error, it can rethrow the error once it has performed whatever cleanup or error handling it needs to perform.
3 件のコメント
Steven Lord
2020 年 11 月 10 日
While I wrote them in that order because I thought of them in that order, in the situation you described I'd prefer the second. Something fatal to the execution of your program occurred, something from which it cannot recover. Having the code throw an error will indicate that the code was unsuccessful in a way that's harder for your user to ignore than a simple displayed message. The red text in the Command Window tends to stand out. Compare:
if 1+1 ~= 3
disp('Incorrect answer')
end
versus
if 1+1 ~= 3
error('Incorrect answer')
end
Which catches your eye more quickly?
Walter Roberson
2020 年 11 月 10 日
If you are using functions and you need to go back up through multiple levels without the function returning its usual value, then you should be using error() . error() can head back up the call stack until it encounters a catch block, and it does not require that each function along the way return anything at all.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!