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:
nargin

Subject: nargin

From: Moon Datta

Date: 5 Feb, 2013 11:07:05

Message: 1 of 8

I am dealing with a m file whose first line is

"if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end"

but when the program is running the it is giving the error

"there must be at least 2 inputs".

but there is already 5 inputs in the worksheet. I have tried a lot but not understanding properly the reason of error. any help from your side will be helpful to me.

thanks in advance.

Subject: nargin

From: Jos (10584)

Date: 5 Feb, 2013 11:26:08

Message: 2 of 8

"Moon Datta" wrote in message <keqp4p$2f9$1@newscl01ah.mathworks.com>...
> I am dealing with a m file whose first line is
>
> "if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end"
>
> but when the program is running the it is giving the error
>
> "there must be at least 2 inputs".
>
> but there is already 5 inputs in the worksheet. I have tried a lot but not understanding properly the reason of error. any help from your side will be helpful to me.
>
> thanks in advance.

nargin is useful inside functions (m-files of which the first non-comment is the word function). Since you say this line is the first line, I suppose you're dealing with a script, for which nargin returns 0, hence the error.

I do not know what you mean by "5 inputs in the worksheet". Do you want to say you have 5 variables in your workspace? And that these 5 variables are the be used by the m-file?

~ Jos

Subject: nargin

From: Moon Datta

Date: 5 Feb, 2013 11:34:08

Message: 3 of 8

"Jos (10584)" wrote in message <keqq8g$63l$1@newscl01ah.mathworks.com>...
> "Moon Datta" wrote in message <keqp4p$2f9$1@newscl01ah.mathworks.com>...
> > I am dealing with a m file whose first line is
> >
> > "if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end"
> >
> > but when the program is running the it is giving the error
> >
> > "there must be at least 2 inputs".
> >
> > but there is already 5 inputs in the worksheet. I have tried a lot but not understanding properly the reason of error. any help from your side will be helpful to me.
> >
> > thanks in advance.
>
> nargin is useful inside functions (m-files of which the first non-comment is the word function). Since you say this line is the first line, I suppose you're dealing with a script, for which nargin returns 0, hence the error.
>
> I do not know what you mean by "5 inputs in the worksheet". Do you want to say you have 5 variables in your workspace? And that these 5 variables are the be used by the m-file?
>
> ~ Jos

Thank you sir.
yes I have 5 variables in my worksheet and they will be used by the function.

Subject: nargin

From: Jos (10584)

Date: 5 Feb, 2013 11:47:07

Message: 4 of 8

"Moon Datta" wrote in message <keqqng$7g3$1@newscl01ah.mathworks.com>...
> "Jos (10584)" wrote in message <keqq8g$63l$1@newscl01ah.mathworks.com>...
> > "Moon Datta" wrote in message <keqp4p$2f9$1@newscl01ah.mathworks.com>...
> > > I am dealing with a m file whose first line is
> > >
> > > "if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end"
> > >
> > > but when the program is running the it is giving the error
> > >
> > > "there must be at least 2 inputs".
> > >
> > > but there is already 5 inputs in the worksheet. I have tried a lot but not understanding properly the reason of error. any help from your side will be helpful to me.
> > >
> > > thanks in advance.
> >
> > nargin is useful inside functions (m-files of which the first non-comment is the word function). Since you say this line is the first line, I suppose you're dealing with a script, for which nargin returns 0, hence the error.
> >
> > I do not know what you mean by "5 inputs in the worksheet". Do you want to say you have 5 variables in your workspace? And that these 5 variables are the be used by the m-file?
> >
> > ~ Jos
>
> Thank you sir.
> yes I have 5 variables in my worksheet and they will be used by the function.

Is your m-file a function or a script? if it is a script you might want to comment out that line ...

~ Jos

Subject: nargin

From: Moon Datta

Date: 6 Feb, 2013 05:56:08

Message: 5 of 8

"Jos (10584)" wrote in message <keqrfr$a21$1@newscl01ah.mathworks.com>...
> "Moon Datta" wrote in message <keqqng$7g3$1@newscl01ah.mathworks.com>...
> > "Jos (10584)" wrote in message <keqq8g$63l$1@newscl01ah.mathworks.com>...
> > > "Moon Datta" wrote in message <keqp4p$2f9$1@newscl01ah.mathworks.com>...
> > > > I am dealing with a m file whose first line is
> > > >
> > > > "if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end"
> > > >
> > > > but when the program is running the it is giving the error
> > > >
> > > > "there must be at least 2 inputs".
> > > >
> > > > but there is already 5 inputs in the worksheet. I have tried a lot but not understanding properly the reason of error. any help from your side will be helpful to me.
> > > >
> > > > thanks in advance.
> > >
> > > nargin is useful inside functions (m-files of which the first non-comment is the word function). Since you say this line is the first line, I suppose you're dealing with a script, for which nargin returns 0, hence the error.
> > >
> > > I do not know what you mean by "5 inputs in the worksheet". Do you want to say you have 5 variables in your workspace? And that these 5 variables are the be used by the m-file?
> > >
> > > ~ Jos
> >
> > Thank you sir.
> > yes I have 5 variables in my worksheet and they will be used by the function.
>
> Is your m-file a function or a script? if it is a script you might want to comment out that line ...
>
> ~ Jos

Sir,
 the m file is a function.

regards
Moon

Subject: nargin

From: James Tursa

Date: 6 Feb, 2013 06:56:08

Message: 6 of 8

"Moon Datta" wrote in message <kesr9o$clj$1@newscl01ah.mathworks.com>...
>
> Sir,
> the m file is a function.

Please post your function.

James Tursa

Subject: nargin

From: Moon Datta

Date: 6 Feb, 2013 11:22:07

Message: 7 of 8

"James Tursa" wrote in message <kesuq8$o6r$1@newscl01ah.mathworks.com>...
> "Moon Datta" wrote in message <kesr9o$clj$1@newscl01ah.mathworks.com>...
> >
> > Sir,
> > the m file is a function.
>
> Please post your function.
>
> James Tursa

sir,
 the function is

function [P,f,alpha] = lomb(x,t,varargin)


x and t are the variables that are present in the workspace. but

if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end


but error is still coming.


regards
Moon

Subject: nargin

From: Jos (10584)

Date: 6 Feb, 2013 13:41:09

Message: 8 of 8

"Moon Datta" wrote in message <ketecv$gto$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <kesuq8$o6r$1@newscl01ah.mathworks.com>...
> > "Moon Datta" wrote in message <kesr9o$clj$1@newscl01ah.mathworks.com>...
> > >
> > > Sir,
> > > the m file is a function.
> >
> > Please post your function.
> >
> > James Tursa
>
> sir,
> the function is
>
> function [P,f,alpha] = lomb(x,t,varargin)
>
>
> x and t are the variables that are present in the workspace. but
>
> if nargin < 2, error('%s: there must be at least 2 inputs.',mfilename); end
>
>
> but error is still coming.
>
>
> regards
> Moon

Note that the function cannot see variables in the workspace. You have to pass them to the function using function arguments. You should call the function like this from the command prompt:
>> lomb(A,B)
where A and B are the names of variables in your workspace. (In your case, it is a little confusing because the names used in the function are the same as the names of the variables outside the function.)

I suspect call this function from the prompt like this:
>> lomb

hth
~ Jos

Tags for this Thread

No tags are associated with 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