Simple MATLAB question

duragezic

Lifer
Oct 11, 1999
11,234
4
81
So I have a very basic prelab that I just wrote but when running the program I get an error that says:

??? Undefined function or variable 'prelab'.

I don't see what's wrong. I have like 4 MATLAB books in front of me and they all have similar code for very basic stuff like I'm trying to do.
 

SinNisTeR

Diamond Member
Jan 3, 2001
3,570
0
0
have you used that variable in another code previously? what about calling functions? make sure that all variables are cleared using the 'clear' function. :confused:
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
It sounds like it's a problem with calling it rather than the code. Paste the signature so we can look at that, too. You can try emptying out the entire body of the function and trying to run it and seeing if that still gives you an error. If so, it's not the code, of course.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Yeah I called it using Debug -> Run. Not sure if that is the correct way to run it... So I just selected the code and dragged it into the command window and lo and behold it worked. I didn't clear anything, that could've been a problem as well. I'll try and keep that stuff in mind, cause I'm a matlab n00b. Thanks all.
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
If the file is subname.m, you can launch it by changing to the directory where it's located and then typing subname (no .m). Functions use the same naming convention.
 

oboeguy

Diamond Member
Dec 7, 1999
3,907
0
76
Originally posted by: duragezic
Yeah I called it using Debug -> Run. Not sure if that is the correct way to run it... So I just selected the code and dragged it into the command window and lo and behold it worked. I didn't clear anything, that could've been a problem as well. I'll try and keep that stuff in mind, cause I'm a matlab n00b. Thanks all.

Ah, this is starting to make some sense. Matlab has its own "path", essentially a list of directories to search for commands issued by the user. It also has a "current directory" (self-explanatory). If you tried to run a script named "prelab" (e.g. if you saved your file with the name "prelab.m") and tried to run it, then most likely you saved it to a directory not on the path and which was not the current directory. Does that clear things up?

BTW, another thing you could have done instead of dragging into the command window is selected the stuff and hit "F9" which evaluates the current selection in the editor. That's what I did when I tested your stuff.