EVALIN cannot execute a return?
显示 更早的评论
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 个评论
Bruno Luong
2020-8-7
编辑:Bruno Luong
2020-8-7
To me it is clear that return cannot be carried out. In order return to carry out, all the pending caliing should be terminated, but the calling function (the one that has EVALIN) is till executing. So no, it's not possible logically, unless the (implicit) RETURN of the CALLING function can somehow go back in time and occurs BEFORE that of the caller. Yes, there seems to be some theoretical about wormhole pretending that can happen and it is possible to go back in time, but no one has yet observed that. ;-)
Caller
|
+-----> Calling
|
+------> Evalin('caller','return') => stuck because Calling is being executing
Fangjun Jiang
2020-8-7
编辑:Bruno Luong
2020-8-7
"return forces MATLAB® to return control to the invoking program before it reaches the end of the script or function"
Who is in control when "return" is executed in this case? I think it is "abort_and_warn". evalin() just gives a different set of workspace. It does not give up control.
Bruno Luong
2020-8-7
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
2020-8-7
采纳的回答
更多回答(1 个)
Fangjun Jiang
2020-8-7
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!