EVALIN cannot execute a return?

5 vues (au cours des 30 derniers jours)
Matt J
Matt J le 7 Août 2020
Commenté : Matt J le 7 Août 2020
I am trying to create a function that aborts whatever mfile is currently executing after issuing a warning. I see no way of avoiding EVALIN for this and so implemented the following,
function abort_and_warn
warning 'Aborting'
evalin('caller', 'return');
end
However, a simple test of this code in R2020a,
function test
disp 'do this'
abort_and_warn
disp 'do that'
end
fails to block the execution of the final line:
>> test
do this
Warning: Aborting
> In test>abort_and_warn (line 12)
In test (line 5)
do that
Is this an undocumented limitation of EVALIN? I see nothing here to imply that EVALIN should be unable to issue the return command in the caller wokspace.
  4 commentaires
Bruno Luong
Bruno Luong le 7 Août 2020
A simple EVAL('RETURN') is not execute either
function foo
eval('return');
fprintf('this will be printed out\n');
end
>> foo
this will be printed out
>>
Matt J
Matt J le 7 Août 2020
Thanks, folks. I guess I'll just stick with
warning 'Aborting...'; return

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 7 Août 2020
Modifié(e) : Bruno Luong le 7 Août 2020
Yes it is written in the doc.
From EVALIN/EVAL document page
...
expression Expression to evaluate
character vector | string scalar
Expression to evaluate, specified as a character vector or string scalar. expression must be a valid MATLAB expression and must not include any MATLAB keywords."
>> iskeyword('return')
ans =
logical
1
  1 commentaire
Bruno Luong
Bruno Luong le 7 Août 2020
Modifié(e) : Bruno Luong le 7 Août 2020
However
eval('global foo')
works and global is a keyword.Go figure....
That exposes few of the hidden paradoxal gems of MATLAB.

Connectez-vous pour commenter.

Plus de réponses (1)

Fangjun Jiang
Fangjun Jiang le 7 Août 2020
It seems that "return" was not executed in the "caller" workspace as it is supposed to by evalin().
But would a "regular" approach work by passing some kind of flag or value?
In function abort_and_warn, return with a flag or a special value after issuing the warning.
In function test, check the return value of function abort_and_warn, then execute return.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by