To execute a command automatically when MATLAB is terminating:
- Execute the code
edit 'finish.m'to open the file in the Editor
- Add the command to the file
- Save the file and verify that it is in the search path
Note: If a file called ‘finish.m’ does not exist, MATLAB will ask to create it. Accept and verify that the file is saved to a location in the search path.
Add to the ‘finish.m’ file the commands that you repetitively execute at the end of each work session with MATLAB. For example:
I recommend the following books to learn more on the MATLAB environment.
This book was the winner of a 2017 Textbook Excellence Award. The first chapter explains the windows of the MATLAB environment, among other introductory topics like operators, random numbers, and trigonometry.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The second chapter explains the windows of the MATLAB environment, how to name variables, and how to save a series of commands in an .M file, among other topics.
I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.
- Getting Help in MATLAB
- Valid Identifiers in MATLAB
- Reserved Words in MATLAB
- Comments in MATLAB
- Multiline Statements in MATLAB
- How to Execute a Command Automatically When MATLAB Starts
- How to Execute a Command Automatically When MATLAB Quits
- How to Save the Workspace Variables to a .MAT File in MATLAB
- How to Load the Variables in a .MAT File into the Workspace in MATLAB
- How to Delete a Variable in MATLAB
I hope you have learned what the ‘finish.m’ file is and how MATLAB uses it. The source code developed in this tutorial is available at this page.