Accessing variables on onCleanUp function

14 views (last 30 days)
Hello everyone,
I Have a function that uses several communication interfaces and instruments control.
Upon finishing the function (completion or cancelation, including CTRL+C), the function should exit and clean up the connections, as well as files that were created.
I used the cleanup object to do this, but ofcourse, unfortunately, it does not recognize local variables since MATLAB already cleaned them.
Example code:
function foo()
cleanupObj = onCleanup(@() cleanMeUp;
%instruments declaration
intrument1 = visa(...)
%folder creation
folder_path = "C:\..."
mkdir(folder_path)
some_code...
function cleanMeUp
if (exist('instrument1','var')
fclose(instrument1)
end
if (exist('folder_path','var'))
rmdir(folder_path, 's')
end
end
end
The problem is that the cleanMeUp function does not recognize instrument1 and folder_path variables as they are local.
I've tried two possible solutions, both aren't good practice and may lead to some errors.
  • Use global variables for the required variables, i.e.
function foo()
global intrument1
global folder_path
cleanupObj = onCleanup(@() cleanMeUp;
...
function cleanMeUp
global intrument1
global folder_path
if (exist('instrument1','var')
fclose(instrument1)
end
if (exist('folder_path','var'))
rmdir(folder_path, 's')
end
end
end
but I've heared this is not a good practice to use global variables, but this solution works.
  • Pass the variables to the cleanMeUp function, i.e.
function foo()
cleanupObj = onCleanup(@() cleanMeUp(intrument1,folder_path));
...
function cleanMeUp(intrument1,folder_path)
if (exist('instrument1','var')
fclose(instrument1)
end
if (exist('folder_path','var'))
rmdir(folder_path, 's')
end
end
end
The problem is that the cleanMeUp function only knows the variables according to the time cleanupObj was created, and if they are changed, it will not work properly.
Any ideas how to do this in an ideal way?
Thanks in advance!

Accepted Answer

Sean de Wolski
Sean de Wolski on 24 Jun 2020
Edited: Sean de Wolski on 24 Jun 2020
function foo()
%instruments declaration
intrument1 = visa(...)
instrumentcloser = onCleanup(@()fclose(instrument1));
%folder creation
folder_path = "C:\..."
mkdir(folder_path)
folderremover = onCleanup(@()rmdir(folder_path, 's'));
end
The cleanup object should only clean up what it knows. If they change (not sure why they would), recreate the cleanup object. Note that I created the instrument and folder before the cleanup object so there's no exist test, and each cleanup object only has one task. Now if your mkdir command fails to run, the instrument will still be closed.
  3 Comments
Sean de Wolski
Sean de Wolski on 24 Jun 2020
If you overwrite the object, then the old one will fire, yes. Why do you need to recreate it?
Yes, I recommend one task per cleanup object (and function in general).
or ohev shalom
or ohev shalom on 24 Jun 2020
Ok, I think I will go with the task per cleanup object.
I needed to recreate it since the status was changed (I won't need it if I use the above suggestion).
Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Instrument Connection and Communication in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!