Potencial Matlab Bugs with keyword close

1 visualización (últimos 30 días)
Fei
Fei el 7 de Mayo de 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.

Respuestas (1)

Image Analyst
Image Analyst el 7 de Mayo de 2013
Editada: Image Analyst el 7 de Mayo de 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 comentarios
Fei
Fei el 7 de Mayo de 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 el 7 de Mayo de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Variables en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by