1

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?

Micah Smith
  • 4,203
  • 22
  • 28
  • In short: the problem is that `dataset` is a function, and running it without input parameters construct a dataset object. If you assign a value to it within your function, MATLAB remembers this. If you do this in the separate script, MATLAB first executes the function. – MeMyselfAndI Oct 16 '14 at 19:30
  • Why does Matlab remember the assigned in the function but not from a separate script? I thought running a script is just like copy-paste of the code therein? Also, why then would the integer value appear in debugging mode but not in normal execution? – Micah Smith Oct 16 '14 at 19:34
  • 1
    I have to say that I dont now all the details, but this has to do with the parsing order in MATLAB. – MeMyselfAndI Oct 16 '14 at 19:47
  • @JandeGier is probably correct about the parsing order. `dataset` must mean something else for matlab because if you change your variable name to something else the error disappear and the code run fine. – Hoki Oct 16 '14 at 19:55
  • Yes, with a different variable name it works, and also by adding the line `dataset=0` before the script run, it works. I would like to understand more what is happening under the hood if anyone knows. – Micah Smith Oct 16 '14 at 20:05
  • To add to the other comments, it's never a good idea to name a variable with the name of a built-in function, or you get unintended behaviour, as demonstrated here... – am304 Oct 16 '14 at 20:22
  • talking about undefined behavior, I would try as much as possible to avoid calling scripts from within a function, The scope of variables may be difficult to assess in some cases. – Hoki Oct 16 '14 at 21:12

1 Answers1

5

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.

TroyHaskin
  • 8,361
  • 3
  • 22
  • 22