Potencial Matlab Bugs with keyword close

1 次查看(过去 30 天)
Hi,
I stumbled across the blow problem.
let's say I generated the below matrix and save to d:
close = rand(10, 10);
cd 'D:/';
save close close
then I use above in a funcion
function result = testClose()
cd 'D:/'
load close;
pclose = close; %now pclose = 1 which is not the value I loaded, seems the
%close is the keyword close instead of the value I loaded
end
As I understand, in the context of the above function ,the close should be the value I loaded from D drive instead of the keyword close, and if I run the "pclose = close" again in the command window, everything works as expected. This should be a bug.
Please advise.

回答(1 个)

Image Analyst
Image Analyst 2013-5-7
编辑:Image Analyst 2013-5-7
Now hold on there. I hesitate to declare something a bug when (1) someone overwrites a built-in function like "close()", or (2) doesn't use the functions properly. This is how you should do it:
function test
randomValues = rand(10, 10)
save('close.mat', 'randomValues');
% Now recall
testClose
function result = testClose()
storedStructure = load('close.mat');
pclose = storedStructure.randomValues
result = pclose;
Of course it works perfectly fine if you do it like you're supposed to.
  3 个评论
Fei
Fei 2013-5-7
Thanks for the quick response. I know how to fix this by the way, my question is "if this behaviour is working as expected".
As the common programming practise, the local variable should be viewed with the highest priority.
load close;%line1
pclose = close; %this close should be the valued load in line1 but not the build-in function
In most of the programming language like java/c#..., variables with the same name are processed as "most recent defined".
If matlab handles variables as above, we have to check every variable names before defining it, which does not make sense.
Walter Roberson
Walter Roberson 2013-5-7
The difficulty is not with it being a keyword. The difficulty is that you used the closure form of "function", with an "end" statement matching the "function" statement. When you do that, MATLAB assumes that there will not be any values "poofed" into existence.... such as by using the command form of "load". You have
load close
If you had changed that to functional form,
data = load('close');
close = data.close;
then you would not have seen the problem. Likewise, Image Analyst assigns to "close" before the load, and that is sufficient to tell MATLAB to use "close" as an array name instead of as a command.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by