function foo()
intrument1 = visa(...
instrumentcloser = onCleanup(@()fclose(instrument1));
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.
Best Answer