Potencial Matlab Bugs with keyword close

1 vue (au cours des 30 derniers jours)
Fei
Fei le 7 Mai 2013
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.

Réponses (1)

Image Analyst
Image Analyst le 7 Mai 2013
Modifié(e) : Image Analyst le 7 Mai 2013
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 commentaires
Fei
Fei le 7 Mai 2013
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 le 7 Mai 2013
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by