MATLAB: Subfunction with output varargout returns only first element of cell array. Why

subfunctionvarargout

This is my script:
a=9;
b=2;
[varargout] = myfunc(a,b)
c=varargout
function [varargout] = myfunc(a,b)
sum=a+b;
dif=a-b;
prod=a*b;
varargout={sum,dif,prod}
end
After execution, the value of c is 11. I want it to be an array, with values 11, 7, 18. {sum, dif,prod}. What is wrong with my script, and how can I get the desired array?

Best Answer

I you only want all the computations bundled into one output argument, then varargout is not the right thing:
function c = myfunc(a,b)
sum=a+b;
dif=a-b;
prod=a*b;
c={sum,dif,prod};