Reputation: 4471
I'm experiencing a puzzling error in Matlab R2012b. It seems that variable names that are also data types exhibit strange behavior. Please see this small example:
function [] = test1()
dataset = 1;
if dataset ~= 0
disp hello
end
end
A call to test1()
produces output hello
, as expected.
Now, rather than set the value of dataset
in my function, I run a script instead.
function [] = test2()
myscript;
if dataset ~= 0
disp hello
end
end
where myscript.m
has one line:
dataset=1;
Now, when I call test2()
I get this error:
Undefined function 'ne' for input arguments of type 'dataset'.
Error in test2 (line 4)
if dataset ~= 0
(Forgive the variable named dataset
- I know that it is also the name of a data type, and it came in the code I was running.) So it seems as if in test2
, Matlab creates an empty dataset
object rather than using the variable named dataset
. Furthermore, this behavior only appears when I set the value in a script rather than in the function body. Even more weird, is that I can do:
>> dbstop in test2 at 4 % line of if statement
>> test2()
K>> dataset
dataset =
1.00
K>> dataset ~= 0
ans =
1
K>> if dataset ~= 0, disp hello; end
hello
K>> dbcont
and I get the same error! The error is not displayed in debugging mode but it is in normal execution.
Can anyone reproduce this? What is going on here?
Upvotes: 1
Views: 143
Reputation: 8401
The MATLAB online help has some pages dealing with this issue; Variables Names and Loading Variables within a Function seem to be the most relevant.
There is no explicit page that discusses how MATLAB resolves names at compilation time, but there is one little tidbit at the bottom of the Variables Names page: "In some cases, load
or eval
add variables that have the same names as functions. Unless these variables are in the function workspace before the call to load
or eval
, the MATLAB parser interprets the variable names as function names."
In other words, if the parser finds an explicit assignment to a variable whose name is the same as another existent object, the local definition takes precedence.
In your test2()
, there is no explicit assignment to a variable dataset
; therefore, when the file is compiled, the parser interprets dataset
to be a class constructor (since the parser will not run or inline myscript
into the function).
Then at run-time, even though a variable named dataset
has been poofed1 into the function's workspace, the interpreted code that is running still has the dataset
symbol in the if
-statement associated with the class constructor.
If you need to, you can still use the dataset
variable name and load from an external file, but it should be done with an explicit assignment via a function call. For example:
dataset = initialize();
Now the parser will notice that dataset
is some arbitrary output of the function initialize
and all will be well. In fact, you can have even have initialize
return a dataset
constructor to the dataset
variable if you wanted.
1 When variables are defined without explicit assignment, MATLAB people (at least on some of their blogs I've read) called this 'poofing'. Using load
without any output arguments, using eval
, and simply running scripts (not functions) can all poof variables into the workspace. This can work fine as long as the variable names do not conflict with other in-use symbols at compile time.
Upvotes: 5