return doesn't work within function

27 views (last 30 days)
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!

Accepted Answer

Matt J
Matt J on 10 Nov 2020
Edited: Matt J on 10 Nov 2020
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 Comments
Matt J
Matt J on 10 Nov 2020
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 on 10 Nov 2020
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!

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 10 Nov 2020
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 Comments
Steven Lord
Steven Lord on 10 Nov 2020
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 on 10 Nov 2020
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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!