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 .

 Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Mayo de 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 comentarios

Akash Pal
Akash Pal el 9 de Mayo de 2022
[Coap,Co1ap,combap,obj1ap,obj2ap,comb1ap,comb3ap,comb4ap,comb5ap,comb6ap] = safe(@proposed2,Xa,i,j);
File: safe.m Line: 3 Column: 16
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
error is showing in this helper function.
function varargout = safe(fcn, varargin)
try
[varargout{:}] = fcn(varargin{:}) ;
catch ME:
varargout(:) = {nan};
end
Akash Pal
Akash Pal el 9 de Mayo de 2022
[F1,M1,deobj1a,bb1,deobj2a,Xa,Coap,Co1ap,combap,obj1ap,obj2ap,comb1ap,comb3ap,comb4ap,comb5ap,comb6ap,comb12ae,obj11ae,obj22ae,comb11ae,comb22ae,comb33ae,comb44ae,comb55ae,comb66ae,t1a,t2a,t4a,t3a,t5a]=safe(@Together2,C,Dij,i,j,Fj,Uij,DQij)
Output argument "varargout{2}" (and maybe others) not assigned during call to "safe".
First i made one extra function called safe ,as guided by you .then i am just calling my subfunction .but its showing this kind or output.
function varargout = safe(fcn, varargin)
try
[varargout{1:nargout}] = fcn(varargin{:}) ;
catch ME:
varargout(1:nargout) = {nan};
end

Iniciar sesión para comentar.

Más respuestas (1)

David Hill
David Hill el 6 de Mayo de 2022
function [out1,out2]=mainfun(input)
out1=nan;out2=nan;
try [out1]=fun1(input);
end
try [out2]=fun2(input);
end

2 comentarios

Akash Pal
Akash Pal el 9 de Mayo de 2022
i tried the ..Its helpfull ,incase of lots of output in the both sub function that time what to do ?should i write also
out1=nan;out2=nan ; for every individual output ?
Walter Roberson
Walter Roberson el 9 de Mayo de 2022
You should initialize any output variable that might otherwise not be assigned to because of errors.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 6 de Mayo de 2022

Comentada:

el 9 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by