Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How to define a function that can have multiple variables.

Asked by C Zeng

C Zeng (view profile)

on 11 Jun 2012

For example f(x,y,z) or f(x,y), z is optional. If I list three variables then system uses f(x,y,z), if only 2 variables uses f(x,y).

I know how to use it by nargin function, is there better way to do so?

Thanks.

0 Comments

C Zeng

C Zeng (view profile)

Products

No products are associated with this question.

1 Answer

Answer by the cyclist

the cyclist (view profile)

on 11 Jun 2012
Accepted answer

When you say you know how to do it by nargin, does that mean you are also familiar with varargin? That sounds like what you want.

>> doc varargin

There is an example on this page:

http://www.mathworks.com/help/techdoc/ref/varargin.html

3 Comments

C Zeng

C Zeng (view profile)

on 11 Jun 2012

Hi, thanks. Yes, I know how to do it by nargin, by writing a if nargin<3 command.

Could you write an example by using varragin?

Thank you!

the cyclist

the cyclist (view profile)

on 12 Jun 2012

I edited my reply to include a web page that has an example.

C Zeng

C Zeng (view profile)

on 12 Jun 2012

thank you!

the cyclist

the cyclist (view profile)

Contact us