필터 지우기
필터 지우기

How do I return to the twice above calling function?

조회 수: 4 (최근 30일)
Amanda Beatty
Amanda Beatty 2023년 3월 17일
이동: Stephen23 2023년 3월 20일
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