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:
undefined variable error message

Subject: undefined variable error message

From: J Bell

Date: 23 Mar, 2010 00:22:23

Message: 1 of 11

This is my code:

function theVal = dumb(beta,Y,X)

err=Y-X*beta;
err=abs(err);
theVal=sum(err);

end

when I run it, I get the message

??? Input argument "Y" is undefined.

Error in ==> dumb at 3
err=Y-X*beta;

any help?

thanks

why does this error message appear in general?

Subject: undefined variable error message

From: Oleg Komarov

Date: 23 Mar, 2010 00:30:13

Message: 2 of 11

> when I run it, I get the message
>
> ??? Input argument "Y" is undefined.
>
> Error in ==> dumb at 3
> err=Y-X*beta;
>
> any help?
>
> thanks
>
> why does this error message appear in general?

Be more precise, how do you run it?

It appears whenewer you try to use a variable that doesn't exist yet in the workspace.

An example:
clear
a = 1+c;

Oleg

Subject: undefined variable error message

From: J Bell

Date: 23 Mar, 2010 00:38:04

Message: 3 of 11

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ho922l$t30$1@fred.mathworks.com>...
> > when I run it, I get the message
> >
> > ??? Input argument "Y" is undefined.
> >
> > Error in ==> dumb at 3
> > err=Y-X*beta;
> >
> > any help?
> >
> > thanks
> >
> > why does this error message appear in general?
>
> Be more precise, how do you run it?
>
> It appears whenewer you try to use a variable that doesn't exist yet in the workspace.
>
> An example:
> clear
> a = 1+c;
>
> Oleg

I saved an m file with the name "dumb.m" containing this code, and in Matlab navigated to the location of this m file so that it appears in the "Current Directory" window. I run it by typing "dumb" and pressing enter in the command window. Perhaps this is the problem?

Subject: undefined variable error message

From: Matt Fig

Date: 23 Mar, 2010 00:38:05

Message: 4 of 11

"J Bell" <superblindman@gmail.com> wrote in message
> ??? Input argument "Y" is undefined.
>
> Error in ==> dumb at 3
> err=Y-X*beta;
> why does this error message appear in general?

Shockingly, this error message, in general, means that one of the arguments the function is supposed to receive when called (known as an 'input' argument), has not been received. This leaves the argument 'undefined' within the workspace of the function.

Subject: undefined variable error message

From: Nathan

Date: 23 Mar, 2010 00:46:00

Message: 5 of 11

On Mar 22, 5:38 pm, "J Bell" <superblind...@gmail.com> wrote:
> "Oleg Komarov" <oleg.komarovRemove.t...@hotmail.it> wrote in message <ho922l$t3...@fred.mathworks.com>...
> > > when I run it, I get the message
>
> > > ??? Input argument "Y" is undefined.
>
> > > Error in ==> dumb at 3
> > > err=Y-X*beta;
>
> > > any help?
>
> > > thanks
>
> > > why does this error message appear in general?  
>
> > Be more precise, how do you run it?
>
> > It appears whenewer you try to use a variable that doesn't exist yet in the workspace.
>
> > An example:
> > clear
> > a = 1+c;
>
> > Oleg
>
> I saved an m file with the name "dumb.m" containing this code, and in Matlab navigated to the location of this m file so that it appears in the "Current Directory" window.  I run it by typing "dumb" and pressing enter in the command window.  Perhaps this is the problem?

That is the problem.

What value would you expect to be in Y, X, or beta, if you don't tell
the program what values they are supposed to be?

If that is the extent of your function, try typing this in the matlab
command window:
%arbitrary data values:
Y = 5;
beta = .01;
X = 4;
%function call
dumb(beta,Y,X)


-Nathan

Subject: undefined variable error message

From: J Bell

Date: 23 Mar, 2010 01:13:05

Message: 6 of 11

Nathan <ngreco32@gmail.com> wrote in message <cd8c0f96-ec6b-47bc-8c0c-2d8b4dd441cb@d30g2000prn.googlegroups.com>...
> On Mar 22, 5:38 pm, "J Bell" <superblind...@gmail.com> wrote:
> > "Oleg Komarov" <oleg.komarovRemove.t...@hotmail.it> wrote in message <ho922l$t3...@fred.mathworks.com>...
> > > > when I run it, I get the message
> >
> > > > ??? Input argument "Y" is undefined.
> >
> > > > Error in ==> dumb at 3
> > > > err=Y-X*beta;
> >
> > > > any help?
> >
> > > > thanks
> >
> > > > why does this error message appear in general?  
> >
> > > Be more precise, how do you run it?
> >
> > > It appears whenewer you try to use a variable that doesn't exist yet in the workspace.
> >
> > > An example:
> > > clear
> > > a = 1+c;
> >
> > > Oleg
> >
> > I saved an m file with the name "dumb.m" containing this code, and in Matlab navigated to the location of this m file so that it appears in the "Current Directory" window.  I run it by typing "dumb" and pressing enter in the command window.  Perhaps this is the problem?
>
> That is the problem.
>
> What value would you expect to be in Y, X, or beta, if you don't tell
> the program what values they are supposed to be?
>
> If that is the extent of your function, try typing this in the matlab
> command window:
> %arbitrary data values:
> Y = 5;
> beta = .01;
> X = 4;
> %function call
> dumb(beta,Y,X)
>
>
> -Nathan

I made this very cumbersome by not including the other code, that which defines the variables, sorry, here it is:

beta=[1;0];
X=[ones(N,1) rand(N,1)];
    mu=randn(N,1);
    Y=X*beta+mu;

So Y is defined here. I should have included this before, sorry.
So the variable "Y" appears in the workspace with the value listed as "<500x1 double>"

After running this in the command window, I still get the error message.

Subject: undefined variable error message

From: Matt Fig

Date: 23 Mar, 2010 01:28:05

Message: 7 of 11

Type this, then hit return:

>>docsearch('function syntax')

then read it all.

Subject: undefined variable error message

From: Walter Roberson

Date: 23 Mar, 2010 01:33:32

Message: 8 of 11

J Bell wrote:

> I made this very cumbersome by not including the other code, that which
> defines the variables, sorry, here it is:
>
> beta=[1;0];
> X=[ones(N,1) rand(N,1)];
> mu=randn(N,1);
> Y=X*beta+mu;
>
> So Y is defined here. I should have included this before, sorry.
> So the variable "Y" appears in the workspace with the value listed as
> "<500x1 double>"

Yup, but that's in the "base workspace" that it is defined, not in the
workspace of your function.

A variable name in your function need not have a single thing to do with
a similar variable name in the base workspace or in the calling
function. The content of the 'Y' variable in your function is determined
  entirely by the _value_ of what you pass to the function in the
corresponding argument *position*.

Just do like an earlier poster suggested, and invoke the function from
the command window, e.g.,

dumb(beta,Y,X)

That would take the _value_ of beta from the current workspace and make
it available to the function 'dumb' under whatever name you listed first
in the arguments in the 'function' statement.

Subject: undefined variable error message

From: Sadik

Date: 23 Mar, 2010 01:34:20

Message: 9 of 11

Hi,

The problem could well be that you have more than one dumb.m's...

Please type the following in the command window:

which -all dumb

This is going to give you the list of all different dumb.m's under different folders.

One solution to your problem could be to go directly to the folder in which your three-input dumb.m resides [the one with syntax dumb(beta,Y,X)].

And the bottom line is that we should never write two or more functions with the same name. :-) [Of course, if that is the case...]

Best.

Subject: undefined variable error message

From: Sadik

Date: 23 Mar, 2010 01:39:05

Message: 10 of 11

By the way, there are good questions to you by Nathan here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/277182

Once you answer them, the issue will probably be resolved.

Subject: undefined variable error message

From: J Bell

Date: 23 Mar, 2010 05:37:21

Message: 11 of 11

"Sadik " <sadik.hava@gmail.com> wrote in message <ho963p$sas$1@fred.mathworks.com>...
> By the way, there are good questions to you by Nathan here:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/277182
>
> Once you answer them, the issue will probably be resolved.

Thanks, these posts have helped me to figure it out, especially reading the information about function syntax suggested by one of the people who posted.

Thanks for the help.

I have new questions about the project I'm doing but I think what I learned is the process of passing arguments from an m file to the workspace but using function syntax.

I now need to learn if you can pass arguments from the workspace to an m file.

No need to reply, I'll work on figuring it out, but if anyone knows I'll definitely read it.

thanks again all

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