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
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.
채택된 답변
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
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
추가 답변 (0개)
참고 항목
카테고리
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!