Scheduling Task at the End of Function Execution

As explained earlier as well, the onCleanup object is always executed upon the function completion, irrespective of its declaration. This property can be used to schedule certain tasks at the end of function execution but, the same task can be constructed as the onCleanup object at anytime in the function definition. See the following example.

Matlab




function geeks
 
%displaying the start of function
disp('Main function opened')   
 
%creating the cleanup object
cleanob = onCleanup(@()cleanup);   
disp('Function Running...')           
end
 
%function to alert the end
%of geeks function execution
function cleanup
disp('Main function Closed and Clean Up done!')
end


Now, if the cleanob was any ordinary object, it would have been executed in the top to bottom approach of execution however, this is not the case as cleanob is a cleanup object thus, it will execute on the end of the geeks function execution. 

Output:

 

Here, we can see that the line 4 executed before the line 3, which should have been the case if cleanob was an ordinary object. Once MATLAB detects that cleanob is a cleanup object, it moves to the following statements, run all the statements in the function definition and then, executes the function handle passed in the onCleanup function i.e, the cleanup function in above example.

Clean Up When Functions Complete in MATLAB

While working in MATLAB, handling the workspaces is crucial for the proper working of multiple scripts and functions. That is why it is a healthy habit to clean up the MATLAB workspace once a function execution is completed.

Some of the requirements for performing a cleanup routine, cleaning up all the files, scripts, and/or functions of the last work environment, could be:

  • Removing all the variables from workspace that are not required.
  • Freeing up any used memory.
  • Removing locks placed upon any file in the MATLAB session.
  • Restoration of the MATLAB path, if it was tempered with.
  • Close any files (scripts or functions) that were opened for access.

In this article, we shall use the onCleanup function of MATLAB to perform different sorts of cleanup tasks in MATLAB.

Syntax

The cleanup function takes a function handle as its input argument, which is performed when MATLAB picks up a cleanup object.

cleanUpObj = onCleanup(function_handle)

The input function handle could be an anonymous function, which generally is the case and executes the input handle once the function containing the onCleanup object is read into the memory.

Now we shall see some uses of the onCleanup function with example.

Similar Reads

Closing up opened files

In this example, we shall see how to close an opened file once the function accessing it, is completely executed....

Scheduling Task at the End of Function Execution

...

Conclusion

As explained earlier as well, the onCleanup object is always executed upon the function completion, irrespective of its declaration. This property can be used to schedule certain tasks at the end of function execution but, the same task can be constructed as the onCleanup object at anytime in the function definition. See the following example....