Potencial Matlab Bugs with keyword close

조회 수: 1 (최근 30일)
Fei
Fei 2013년 5월 7일
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