Accessing variables on onCleanUp function

7 ビュー (過去 30 日間)
or ohev shalom
or ohev shalom 2020 年 6 月 24 日
コメント済み: or ohev shalom 2020 年 6 月 24 日
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!

採用された回答

Sean de Wolski
Sean de Wolski 2020 年 6 月 24 日
編集済み: Sean de Wolski 2020 年 6 月 24 日
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 件のコメント
Sean de Wolski
Sean de Wolski 2020 年 6 月 24 日
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 2020 年 6 月 24 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInstrument Connection and Communication についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by