when combing OnCleanup with evalin, weird order of execution

1 Ansicht (letzte 30 Tage)
raym
raym am 9 Nov. 2019
Bearbeitet: raym am 9 Nov. 2019
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

Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by