I want to build a matlab function with outputs whose number/length is variable. Below is my code, which somehow returns an error:
function varargout=polyargout(x)if x==1 nargout=1; varargout{1}=1;elseif x==2 nargout=2; for i=1:x varargout{i}=magic(i); endend
Basically one doesn't know how many outputs to return until running the function. To call the function, I wish to use:
y=polyargout(1) to get y=1
y=polyargout(2) to get y=[magic(1),magic(2)]
Best Answer