MATLAB: Is there a cleanup or finally type construct in the MATLAB language for end of function clean up

.mcleanupcodedestructorerrorfinallyMATLAB

In a lot of our programming work we need to clean up (delete temp files, change file permissions, release disabled GUI controls, etc) after our functions if it quits successfully, unexpectedly or by the 'return' keyword.
Is there syntax similar to the functionality in a C++ Object Destructor? Similar to the following destructor block (hence 'destruct' becomes a keyword in MATLAB functions):
function foo(...)
% Main program goes here
destruct
% Cleanup routines
end
For any reason the function quits (errors, 'return', or reached the end), the contents of the destructor/finally block will be executed.

Best Answer

There is no destructor or finally keyword or construct in MATLAB for cleaning up an end of function.
In order to do this there is a function available in MATLAB 7.6(R2008a) named ONCLEANUP. This function can be used to cleanup tasks at function completion.
There is also an ability within the MATLAB Object System to have a destructor as is illustrated in the following code segment.
% myClass.m
classdef myClass < handle
properties
a
end
methods
function obj = myClass(a)
obj.a = a;
end
function delete(obj)
disp('Getting deleted');
end
end
end
% end of class
This would allow the user to call a delete function and "cleanup" any rogue variables.