Save this sample function as test_func.m
:
function test_func
load test.mat
whos
alpha
and run this sample script:
alpha = 3;
save test.mat
test_func
Here is the output I get:
Name Size Bytes Class Attributes
alpha 1x1 8 double
??? Error using ==> alpha at 40
Not enough input arguments.
Error in ==> test_func at 4
alpha
The output of whos
shows that the variable is loaded in the local workspace of the function. I can even put a breakpoint on line 4 of test_func
and type alpha
and I get the correct result, but as soon as I do a step forward with the debugger, it fails again: the function alpha
is masking the local variable and I don't see why.
Replacing the content of test_func
by
p = load('test.mat');
p.alpha
works fine but that's not what I'm trying to do. I would like to load variables directly inside the local workspace of the function.
To me it looks like a bug (I'm using Matlab R2011a) but if it's a feature can you explain it and help me find a workaround?
Heh, you might be right that this is a bug. Looks really strange, because the following works
function test_func
load('test.mat','alpha');
whos
alpha
Another thing that helps is to initialize the variable before a call to load
function test_func
alpha = 0;
load('test.mat');
whos
alpha