Calling evalin with three arguments
6 次查看(过去 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?
0 个评论
采纳的回答
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 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!