when combing OnCleanup with evalin, weird order of execution

1 visualización (últimos 30 días)
raym
raym el 9 de Nov. de 2019
Editada: raym el 9 de Nov. de 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

Respuestas (0)

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by