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
  5 个评论
Robyn Galliers
Robyn Galliers 2025-7-10
Hi Stephen,
Thanks for your further reply. I am not sure what a MWE is, and googling it doesn't bring up any obvious answers. But if it is some sort of web example code, I unfortunately do not have time to code anything else up, my dissertation is due in less than 2 weeks, so I will just have to live with the behaviour and manually run cleanup commands from the console. I was expecting to have missed something obvious and had hoped I could make a quick change to rectify it.
Thanks for your input in any case.
Regards

请先登录,再进行评论。

回答(1 个)

Steven Lord
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.
  1. The function completes its execution normally, and its workspace (including the object) gets destroyed as part of the function returning to its caller.
  2. 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.
  3. 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
Goodbye
x = 42
y = onCleanup(@() disp("Farewell cruel world!"));
delete(y)
Farewell cruel world!
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.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by