input argument is undefined

10 Ansichten (letzte 30 Tage)
Moe
Moe am 1 Dez. 2011
Hi, whenever I run the function below, I get that the input is undefined, although when I try the function in the command window it works perfectly, so I am wondering why do I get the message at the beginning which tells me input is undefined.
function [pow_table] = square_table(N)
array = 1:N
pow_table = array.^2;
end
Thank you

Antworten (1)

Matt Tearle
Matt Tearle am 1 Dez. 2011
When you say "run the function", what do you mean -- you hit F5 or push the save/run button (with the "play" icon) in the Editor? Because that is equivalent to calling it at the command line without an input:
>> square_table
So, yes, N is undefined, hence the error.
EDIT TO ADD (based on comments below): When called directly from the command line
>> foo = square_table(7)
MATLAB will go through a standard look-up procedure to work out what square_table represents. First, is it a local variable? If not, is a function or script (command)? This also requires looking in a prescribed set of places. The simple version is: first look in the current directory, then look in the directories specified by the MATLAB path (in order).
Hence, if you write square_table.m and save it in C:\work\Moe\MATLAB then that directory needs to be either the current working directory on on the MATLAB path. Otherwise you will get an error ("undefined function or variable").
The current directory is shown at the top of the MATLAB environment ("current folder"). The files are also shown in the Current Folder Browser, which is the left-hand panel in the default desktop arrangement. Finally, you can also type pwd in the Command Window to return it.
Type pathtool in the Command Window to bring up the Set Path dialog. This will show you the current MATLAB path. It will probably have a whole bunch of directories like C:\Program Files\MATLAB\R2012a\toolbox\matlab\graphics on it. This is obviously the MATLAB installation itself, where it finds the MATLAB functionality, like sin and hist and xlsread. You can add your own directories to this path. Note that it works top-to-bottom. So if foo.m exists in two different directories on the path, the one in the higher-listed directory is the one that MATLAB will find.
There are some other subtleties, but that will cover 99% of cases.
The which command is your friend:
>> which square_table
>> which square_table -all
Finally, when you hit F5 or the "play" button in the Editor, it will automatically give you the chance to change directories or change the path, if the file isn't already on the path. That's why you were able to use that to "make the function work" (but with the undefined input error).
  3 Kommentare
Matt Tearle
Matt Tearle am 1 Dez. 2011
What do you mean "make the function defined in the command window"? In your initial post you said it works when you try it in the command window. I'm obviously missing something.
What happens if you enter >> square_table(7) in the command window?
Are you perhaps referring to a path issue? It won't work when you first write the function ("undefined function or variable 'square_table'"), but after hitting the run button (and getting an error) you can get it to work at the command line (>> square_table(7))?
Moe
Moe am 3 Dez. 2011
Yes exactly!
When I press square_table(7) in the command window it works perfectly, but still, before that I have to press the run button in the script window and when I press the run button, it gives that error, yet it works.
And can you explain what path issue might be the problem?
Thanks for your time.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by