How do I return to the twice above calling function?

7 次查看(过去 30 天)
I have some nested function calls, starting with a button pushed callback. I inherited the code so this is how it was setup prior to me starting it.
I check to see if a column is missing, and if it is I want to exit the function AND the function that is calling it to get to the original function. Here is the current setup (each function is in different scripts):
function ButtonPushed() %this is the callback for ~70 buttons on my GUI
%About 100 lines of code to set everything up for the triage function
%call triage function
app = TriageFunction(app)
end
function app = TriageFunction(app)
%do different stuff depending on which button was pushed
switch buttonName
case button1
info = abc; %assign some info specific to this button
[error,app] = CheckforErrors(info) %call the CheckforErrors function
if error
return %return to ButtonPushed()
end
app = executeButton1(app) %download information depending on the button
case button2
info = efg; %assign some info specific to this button
[error,app] = CheckforErrors(info) %call the CheckforErrors function
if error
return %returnto ButtonPushed()
end
app = executeButton1(app) %download information depending on the button
end
end
function [error,app] = CheckforErrors(info)
if %[check for error]
%return to TriageFunction() if there's an error
return %HERE IS WHERE I WANT TO RETURN TO ButtonPushed() instead of TriageFunction()
else
%import different data into "app" variable based off info
app = AddNewInfo;
end
end
So if the code catches an error during CheckforErrors() it returns to TriageFunction(). If it sees there was an error, it returns to ButtonPushed(), otherwise it continues to download information. This works, but then I have to have this statement almost 70 times in the switch/case statements:
if error
return
end
I would rather just return to ButtonPushed() function straight from CheckforErrors() to avoid all the if statements. Is this possible?
  2 个评论
Rik
Rik 2023-3-18
I don't think there is a good way to do this. Perhaps the only way would be to use evalin, but you really should want to avoid that. I would prefer to splatter that line of code all over the file instead of using evalin.
Amanda Beatty
Amanda Beatty 2023-3-20
@Rik Okay that's great to know. I guess I'll go ahead and do that. I was hoping there was a simple way of doing it, but if not I'll just copy/paste a bunch. Thanks for your input!

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2023-3-20
Rather than having your CheckforErrors function return true or false as the first output argument I'd have it throw an error using the error function. [This will require not using error as the name of a variable in CheckforErrors.] If your TriageFunction doesn't catch the error by calling CheckforErrors inside a try / catch block, the next function up in the calling stack gets a chance to catch it and so on until the error is caught and not rethrown or until it reaches the top level of the stack.
So have ButtonPushed call TriageFunction inside a try / catch and have CheckforErrors throw errors instead of returning a "status code". You could handle different errors differently by checking the identifier property of the exception object that catch catches (in the example below, ME.identifier.)
try
z = example1930780
catch ME
fprintf("Error with identifier %s caught.\nError message is: %s.\n", ME.identifier, ME.message)
end
Error with identifier myErrorID:inputsNotSameSize caught. Error message is: The inputs to callHelperFunction must be the same size!.
function z = example1930780
x = ones(3);
y = ones(4);
z = callHelperFunction(x, y)
end
function z = callHelperFunction(x, y)
if ~isequal(size(x), size(y))
error("myErrorID:inputsNotSameSize", "The inputs to callHelperFunction must be the same size!")
end
z = x + y;
end
  1 个评论
Amanda Beatty
Amanda Beatty 2023-3-20
移动:Stephen23 2023-3-20
Ahhh, I see what you're saying. Yeah that looks like the right way to do things. Currently I have try/catch statements in both checkForErrors() and in every single executeButton() function, so it is actually less time consuming for me to just copy/paste my "if error" statment 70 times than to go into all 70 executeButton() functions and delete their try catch statements in favor of one big try/catch higher up in the stack. I will probably do that in the future to clean it up though.
Thanks for your answer!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by