Calling evalin with three arguments

6 Ansichten (letzte 30 Tage)
Luca Tarasi
Luca Tarasi am 29 Aug. 2019
Kommentiert: Luca Tarasi am 29 Aug. 2019
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?

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Aug. 2019
Bearbeitet: Stephen23 am 29 Aug. 2019
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 Kommentar
Luca Tarasi
Luca Tarasi am 29 Aug. 2019
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 29 Aug. 2019
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 Kommentar
Luca Tarasi
Luca Tarasi am 29 Aug. 2019
Thank you.
Oddly, in R14 the third argument is still documented in evalin alone. It was dropped in R14SP2, though.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by