when combing OnCleanup with evalin, weird order of execution

1 回表示 (過去 30 日間)
raym
raym 2019 年 11 月 9 日
編集済み: raym 2019 年 11 月 9 日
This function is to print in cmd window that the function was executed and exited.
The expected outcome is :
1
$$$$$$-Entering [testPrologue]
2
$$$$$$-Leaving [testPrologue]
If the fucntion prologue is nested inside the main function, this was true.
However, if I remove the prologue function and make a new file using this fucniton, then print order changed:
1
$$$$$$-Entering [testPrologue]
$$$$$$-Leaving [testPrologue]
2
Why the "$$$$$$-Leaving" was printed earlier even when "2" has not been printed?
In order to make prologue an independent function, how can I modify it to make it work?
Thanks
function testPrologue
clc;
disp(1);
prologue;
disp(2);
%%
function prologue
aStr1 = ['disp(''$$$$$$-Entering testPrologue'');'];
aStr2 = ['onCleanup(@() disp(''$$$$$$-Leaving [testPrologue]''));'];
aStr = sprintf('%s\n%s',aStr1,aStr2);
evalin('caller',aStr);
end
end

回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by