How to get a one function result?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Akash Pal
am 6 Mai 2022
Kommentiert: Walter Roberson
am 9 Mai 2022
I have a main function called mainfun
function [out1,out2]=mainfun(input)
[out1]=fun1(input);
[out2]=fun2(input);
end
so my question is ,may be i am calling my mainfun,and I want to display the individual result from different sub function,but how to display if there is any error in one subfunction ?
like an example ,
if in my fun1 there is eroor then also i want to get the fun2 result .
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 9 Mai 2022
You can create a helper function
function varargout = safe(fcn, varargin)
try
[varargout{}] = fcn(varargin{:}) ;
catch ME:
varargout(:) = {nan};
end
With that you can
[out1] = safe(@fun1,input);
[out2] = safe(@fun2,input);
4 Kommentare
Walter Roberson
am 9 Mai 2022
function varargout = safe(fcn, varargin)
try
[varargout{1:nargout}] = fcn(varargin{:}) ;
catch ME:
varargout(1:nargout) = {nan};
end
Weitere Antworten (1)
David Hill
am 6 Mai 2022
function [out1,out2]=mainfun(input)
out1=nan;out2=nan;
try [out1]=fun1(input);
end
try [out2]=fun2(input);
end
2 Kommentare
Walter Roberson
am 9 Mai 2022
You should initialize any output variable that might otherwise not be assigned to because of errors.
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!