Calling evalin with three arguments

8 次查看(过去 30 天)
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