What is wrong with the code?

조회 수: 1 (최근 30일)
aditya
aditya 2011년 5월 4일
I have this small code:
function test
L = 10;
t = existing_chk('L',uigetdir)
function x = existing_chk(name_x,a)
chk = exist(name_x,'var')
if chk == 0;
x = a;
else
x = eval(name_x);
end
end
end
The problem is that uigetdir is executed even though 'L' exists. I cannot figure out the problem

채택된 답변

Kelly Kearney
Kelly Kearney 2011년 5월 4일
The uigetdir function is being called on line 2 of your function, prior to existing_chk ever being called, since you have not enclosed the function name in quotes. The second argument of existing_chk should be a string. Replace that line with
t = existing_chk('L','uigetdir')
You should probably avoid using eval too, but that's just general programming advice; the function should work as written.
  댓글 수: 1
aditya
aditya 2011년 5월 4일
Thanks Kelly.
Another thing - is there an alternative in this case to eval ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Daniel Shub
Daniel Shub 2011년 5월 4일
MATLAB evaluates uigetdir before existing_chk, so that it can pass whatever uigetdir returns to existing_chk. Either turn the second argument into a function handle @()uigetdir and call a() or turn it into a string 'uigetdir' and call eval(a).

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by