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 CenterFile Exchange에서 Error Handling에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by