MATLAB: How to make function that sometimes return output and how to call

matlab function

  • b sometimes returned from myfn and sometimes not returned, so how to write the function that may retrun b
function [a,b] = myfn(x,y)
a
if(x=y)
b
end
  • also how to call myfn from the main file if b is not returned
[a,b]=myfn(x,y)

Best Answer

Simplest solution: set a default output value:
function [a,b] = myfn(x,y)
a = 1;
b = [];
if(x=y)
b = 2;
end
end