필터 지우기
필터 지우기

Calling evalin with three arguments

조회 수: 8 (최근 30일)
Luca Tarasi
Luca Tarasi 2019년 8월 29일
댓글: Luca Tarasi 2019년 8월 29일
Hi,
according to the documentation for the evalin function, two arguments need to be given when calling it: ws, a string indicating the workspace, and expression, which is a character vector or a string scalar
By mistake, instead of writing
evalin('base','var_name') == 1
I wrote
evalin('base','var_name',1)
To my surprise, this call is accepted, although it just executes as if the third parameter doesn't exist. In fact, by passing pretty much anything as a third parameter, the call works just fine as if it had not been passed (or so it looked to me).
So, what is this third parameter?

채택된 답변

Stephen23
Stephen23 2019년 8월 29일
편집: Stephen23 2019년 8월 29일
The third (as far as I can tell) totally undocumented input argument provides a default value if a specific variable is not found. For example:
function foo()
disp(evalin('base','x','23'))
end
and then
>> clear()
>> foo()
23
>> x = 5;
>> foo()
5
Of course in actual code, it is much better to use efficient methods of passing data between workspaces, rather than slow and obfuscated evalin, in particular passing input/output arguments or using nested functions:
  댓글 수: 1
Luca Tarasi
Luca Tarasi 2019년 8월 29일
I see.
By the way, if I pass something that is not a string or a character as the third parameter, error is given only if the specific variable is not found: I guess that is because, if the specific variable is found, the third parameter is not even looked at.
Thank you.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 8월 29일
In (much) older releases of MATLAB the eval and evalin functions were documented with one more input argument than they are documented to accept now. If the main expression to be evaluated threw an error, that last input argument would be evaluated much like you'd evaluated your main expression in the try section and the last input argument in the catch section of a try / catch block.
While that functionality does remain for backwards compatibility, we removed it from the documentation. A quick check of the archived documentation shows that it was documented in release R13SP2 and not documented in release R14, so it was removed from the documentation fifteen years ago.
  댓글 수: 1
Luca Tarasi
Luca Tarasi 2019년 8월 29일
Thank you.
Oddly, in R14 the third argument is still documented in evalin alone. It was dropped in R14SP2, though.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by