return doesn't work within function

9 次查看(过去 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!

采纳的回答

Matt J
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
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.
Jonas Thomalla
Jonas Thomalla 2020-11-10
I put it at the end of the function because I thought it would also stop execution the script above. I have several conditions that are calling this function and should stop the whole script. Then I could've let return within the function. Now I'm taking your variant and put it everywhere right after calling the function.
Thank you for your help!

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
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:
  1. 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.
  2. 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
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
Incorrect answer
versus
if 1+1 ~= 3
error('Incorrect answer')
end
Incorrect answer
Which catches your eye more quickly?
Walter Roberson
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 CenterFile Exchange 中查找有关 Application Deployment 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by