필터 지우기
필터 지우기

functions take preference to assignin/evalin variables

조회 수: 3 (최근 30일)
po,gpo,eoeo
po,gpo,eoeo 2020년 6월 9일
편집: Stephen23 2020년 6월 9일
Can someone explain to me why the code below outputs 'test from function', indicating that the function takes preference to a variable assigned by assignin to the parent function scope? This just seems like a bug to me.......
function display_weirdness()
assign_weird();
disp(test)
end
function assign_weird
assignin('caller','test','test')
end
function t=test
t='test from function';
end
  댓글 수: 1
Stephen23
Stephen23 2020년 6월 9일
편집: Stephen23 2020년 6월 9일
"...indicating that the function takes preference to a variable assigned by assignin to the parent function scope?"
Yes.
For good reason: because that is more efficient for the JIT engine. If the JIT engine always had to keep looking for magically created variables (e.g. that shadow functions, as your example does) and then when it finds them it would have to replace whatever it had already compiled with something entirely new and compile everything again, then clearly this would slow things down considerably.
This topic has been discussed plenty of times before:
Summary: passing data using the recommended methods is more efficient and less buggy than magically creating variables.

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 9일
편집: Ameer Hamza 2020년 6월 9일
Yes, this is an inconsistent behavior similar to this question: https://www.mathworks.com/matlabcentral/answers/10727-bug-regarding-precedence-of-variables-over-functions-sharing-the-same-name, but it is not considered a bug, as explained by Walter's answer here: https://www.mathworks.com/matlabcentral/answers/10727-bug-regarding-precedence-of-variables-over-functions-sharing-the-same-name#answer_15521. It is a consequence of JIT. Following will work correctly
function display_weirdness()
test = '';
assign_weird();
disp(test)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by