I don't understand how you are meant to use onCleanup in a script
9 次查看(过去 30 天)
显示 更早的评论
I am struggling to understand how to use onCleanup in a script. I have read the help files pretty thoroughly, looked through some other answers on the forum (e.g. https://uk.mathworks.com/matlabcentral/answers/12536-oncleanup-and-script?s_tid=srchtitle, https://uk.mathworks.com/matlabcentral/answers/600571-how-to-use-oncleanup?s_tid=srchtitle) and attempted my own code, but it seems incompatible with what I am trying to achieve. All that I want to do is to perform some tasks when I push CTRL+C or STOP on the taskbar.
I understand that the onCleanup fires when the variable goes out of scope. But then how do you use it in a script that is calling functions? E.g. some psuedo-code below, my main script runs along doing things, sometimes calling a helper function for a discrete complicated bit of code. At that point the cleanup fires because I have now changed workspace.
function MyScriptName()
sPort = serialport("COM4",57600);
fopen(sPort);
cleanupObj = onCleanup(@()ShutDown(sPort));
% Do thing here
% Do another thing
[x,y] = HelperFunction %Cleanup routine will fire on this line
while true
% Do things repetitively until I terminate
[a,b] = AnotherHelperFunction
end
end
x, y = function HelperFunction()
% Do something specific
end
a, b = function AnotherHelperFunction()
% Do something specific
end
% Close down gracefully and safely
function ShutDown(sPort)
write(sPort,data, "uint8");
write(sPort,data, "uint8");
end
So what am I missing? Is it just not possible in my use case? And why does it seem so complicated to just have a top-level "On Stop run this code"?
EDIT: I should add that I first tried it without a main function, but then it seemed to execute immediately. However the first article I linked above said that I needed to use it within a function, so I changed the main body into a function.
Many thanks,
Robyn
回答(1 个)
Steven Lord
2025-7-10
The purpose of an onCleanup object is to execute the function handle with which it was created when the onCleanup object gets destroyed. If you create the onCleanup object inside a function, assuming the onCleanup object is not returned from that function [1], the object will be destroyed under one of three circumstances.
- The function completes its execution normally, and its workspace (including the object) gets destroyed as part of the function returning to its caller.
- The function terminate due to an uncaught error or Ctrl-C interrupt, and its workspace gets destroyed as part of the function returning to the Command Window prompt.
- You explicitly delete or overwrite the variable.
If you create the onCleanup object inside a script file, it doesn't have its own workspace so circumstances 1 and 2 don't apply. You'd need to explicitly destroy the onCleanup object with clear or delete. You could also overwrite it.
x = onCleanup(@() disp("Goodbye"));
x = 42
y = onCleanup(@() disp("Farewell cruel world!"));
delete(y)
FYI, the term "MWE" stands for Minimal Working Example: a small (ideally the smallest) example that others can run to reproduce the behavior you're seeing. What I wrote above is two MWEs, one showing the overwrite case (the x MWE) and the other showing the delete case (the y MWE)
[1] There are a few other ways it could be kept alive, but they're probably rarer. One that might apply in your case is storing the onCleanup in a property of an object that "survives" the function exit, like the UserData property of a figure window.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!