看来我的MATLAB名称与我的一个名为"annotation"的变量和内置的MATLAB函数"annotation"发生冲突.
在我的函数中,我正在加载包含变量注释的.mat文件,然后尝试将其用作另一个函数的参数.最小的工作示例如下所示:
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
Run Code Online (Sandbox Code Playgroud)
现在,在我的函数准备中,我确保该文件包含一个名为"annotation"的变量.当我在main函数的循环中加载它时,"which"命令告诉我它作为变量存在,但在调用other_function时,MATLAB尝试调用函数"annotation":
注释是一个变量.
??? 在71处使用==>注释时出错
没有足够的输入参数
错误==> 14点测试
Run Code Online (Sandbox Code Playgroud)other_function(annotation);
我很困惑,因为我在程序的几个部分使用变量名"annotation",也作为函数调用中的参数.我能想象的唯一解释是MATLAB以某种方式预编译我的代码 - 在"编译时",变量"annotation"不可见.但是,在运行时,可以从"which"命令的输出中看到它.
任何帮助将不胜感激!提前谢谢了.
注意:我使用的是MATLAB 7.12.0(R2011a).
这有点奇怪!我发现了同样的事情。基本上,工作区变量不应位于函数内部的范围内(尽管它们来自脚本内部)。
如果执行load(out_file)此操作,则会将该文件的内容加载到工作区中。所以我认为它们不应该在范围内。因此,我很惊讶将which(annotation)其称为变量,但对于annotation超出范围并不感到惊讶。(实际上,Matlab 似乎确实将变量放入了作用域中。)
我认为你关于某种预处理的想法听起来annotation似乎有道理。例如,如果您替换other_function(annotation)为eval('other_function(annotation);'),那么它可能会起作用(尽管我并不是说您应该使用eval,永远)。
解决此问题的最佳方法是执行以下操作:
data = load(out_file);
annotation = data.annotation;
Run Code Online (Sandbox Code Playgroud)
因此,加载out_file到结构中,然后从那里访问变量。