I wanna define function which has flexible number of argument.

조회 수: 3 (최근 30일)
Dong-Gyu Jang
Dong-Gyu Jang 2014년 12월 26일
댓글: Dong-Gyu Jang 2014년 12월 30일
Hello and happy new year!
Now I'm considering to define function which has flexible arguments.
For example, let's have function F which full argument number is 3 such as F(a,b,c)
If b is 0, user don't need to assign c thus F(a,b) is enough. Otherwise full argument assignment is required.
Can I make this kind of flexible function?

채택된 답변

Shoaibur Rahman
Shoaibur Rahman 2014년 12월 26일
편집: Shoaibur Rahman 2014년 12월 26일
function out = F(varargin)
% do stuffs
% y = varargin{1}+varargin{2}
% out = y+1; etc.
For flexible number of outputs:
function varargout = F(varargin)
% do stuffs
% varargout{1} = varargin{1}-varargin{2}
% etc.
  댓글 수: 3
Shoaibur Rahman
Shoaibur Rahman 2014년 12월 29일
This should work in this way. Anyway, it would be better to see the part of your code and the errors generated by the code.
Dong-Gyu Jang
Dong-Gyu Jang 2014년 12월 30일
Oh, you're right. I've just not known varargin and used arbitary variable name. As the result, it was not operated! Thanks to your advice! my function is now upgraded:)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by