It seems I have a MATLAB name clash with one of my variables called "annotation" and the built-in MATLAB function "annotation".
In my function, I am loading a .mat file containing the variable annotation and then try to use it as an argument to another function. A minimal working example looks like this:
function test()
filenames = { 'file1.mat', 'file2.mat', 'file3.mat' };
for i = 1:numel(filenames)
in_file = char(filenames{i});
out_file = strrep(in_file, '.mat', '_out.mat');
prepare(out_file); % do something with the out file
load(out_file); % contains one variable named "annotation"
which annotation % just to be sure
other_function(annotation);
end
end
function prepare(filename)
annotation = rand(25, 1);
save(filename);
end
function other_function(annotation)
whos % just a stub - see whether it has been called
end
Now, in my function prepare I made sure the file contains a variable named "annotation". When I load it in the main function's loop, the "which" command tells me it exists as a variable, but in the call of other_function, MATLAB tries to call the function "annotation":
annotation is a variable.
??? Error using ==> annotation at 71
Not enough input arguments
Error in ==> test at 14
other_function(annotation);
I am confused because I am using the variable name "annotation" in several parts of my program, also as parameter in function calls. The only explanation I can imagine is that MATLAB somehow precompiles my code - at "compile time", the variable "annotation" is not visible. However, at runtime it is found as can be seen from the output of the "which" command.
Any help would be greatly appreciated! Many thanks in advance.
Note: I am using MATLAB 7.12.0 (R2011a).
That is a beautifully obscure problem! It is crappy design on Mathwork's part. I would even call it a bug, interesting to see if they agree.
The short answer: you can "fix" this by adding the line annnotation = 2;
anywhere in your code above your load(out_file);
line. Or annotation = "roger";
or annotation = false;
, it doesn't matter what type of variable you make annotation, as long as you explicitly force it to be a variable in your code.
Your code as written does not explicitly reference the variable annotation
. annotation
just happens to be the name of a variable in the matlab file you load in to the function's workspace. Somehow, this doesn't throw a runtime error, it just works wrong, which I would call a bug, but matlab might say is a documented limitation. See their documentation at http://www.mathworks.com/help/techdoc/matlab_prog/f4-39683.html#f4-75258 and tell me what you think. That documentation seems to apply to nested functions, which your main function certainly is not. CLEARLY your line other_function(annotation)
should see annotation
in the same scope as which annotation
placed immediately above it sees. (I just did that test and it says annotation is a variable
).
Here is a minimal program that shows the problem:
function test()
prepare('test.mat'); % writes i
load('test.mat'); % contains one variable named "annotation"
which annotation
other_function(annotation);
end
function prepare(filename)
annotation = 42; % the answer is 42
save(filename);
end
function other_function(poodle)
disp(poodle);
end
I invite you to submit this as a bug report at http://www.mathworks.com/support/bugreports using the "report bug" link on that page! If you don't want to, I'll report it, just let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With