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

조회 수: 4(최근 30일)
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?

채택된 답변

Matt J
Matt J 2017년 8월 31일
편집: Matt J 2017년 8월 31일
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};
  댓글 수: 3

댓글을 달려면 로그인하십시오.

추가 답변(0개)

범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by