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 Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by