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

Thread Subject:
How to put a variable number of input arguement ?

Subject: How to put a variable number of input arguement ?

From: sk maidul haque

Date: 3 Jan, 2011 07:58:06

Message: 1 of 3

How to put a variable number of input arguement ?
Suppose I write a program like this.

function function1(a,b)
b=5;
c=a+b

I want to run this program by calling function1(the value of a only) and by default
it should take the value of b as assigned within the program i.e. b=5. As well as it should have the provision to call by function1(a1,b1), when required.
so that it takes a=a1 and b=b1. Please let me know how to put it in the matlab program.

Subject: How to put a variable number of input arguement ?

From: Greg von Winckel

Date: 3 Jan, 2011 11:42:04

Message: 2 of 3

Try

help varargin

hth,

Greg

"sk maidul haque" <skmaidulhaque@gmail.com> wrote in message <ifrvie$m2r$1@fred.mathworks.com>...
> How to put a variable number of input arguement ?
> Suppose I write a program like this.
>
> function function1(a,b)
> b=5;
> c=a+b
>
> I want to run this program by calling function1(the value of a only) and by default
> it should take the value of b as assigned within the program i.e. b=5. As well as it should have the provision to call by function1(a1,b1), when required.
> so that it takes a=a1 and b=b1. Please let me know how to put it in the matlab program.

Subject: How to put a variable number of input arguement ?

From: Oleg Komarov

Date: 3 Jan, 2011 12:11:05

Message: 3 of 3

"sk maidul haque" <skmaidulhaque@gmail.com> wrote in message <ifrvie$m2r$1@fred.mathworks.com>...
> How to put a variable number of input arguement ?
> Suppose I write a program like this.
>
> function function1(a,b)
> b=5;
> c=a+b
>
> I want to run this program by calling function1(the value of a only) and by default
> it should take the value of b as assigned within the program i.e. b=5. As well as it should have the provision to call by function1(a1,b1), when required.
> so that it takes a=a1 and b=b1. Please let me know how to put it in the matlab program.

function foo(a,b)
if nargin == 1
b = 5;
end
...
% rest of your function
end

You may also use varargin as already pointed out but that may be a little over your scope.

Oleg

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us