Main Content

finish

User-defined termination script for MATLAB

Syntax

Description

finish executes user-specified commands when exiting. MATLAB® executes any file on the search path named finish that has an executable file extension. Examples of an executable file extension are .m, .mlx, .p, and .mlapp. MATLAB calls finish, if defined, whenever you do one of the following.

  • Click the close button on the MATLAB desktop on Microsoft® Windows® platforms or the equivalent on UNIX® platforms.

  • Type quit or exit at the command prompt.

Create a finish.m file in the userpath folder, which is on the MATLAB search path. Add commands you want executed at exit.

If an error occurs in the finish script, then quit is canceled so that you can correct your finish file without losing your workspace.

example

Examples

collapse all

Use the MATLAB sample file finishsav.m to save the workspace to a MAT-file when MATLAB quits.

If there is a finish.m file in your userpath folder, open it.

edit(fullfile(userpath,'finish.m'))

Otherwise, create a function named finish with no inputs or outputs and document the functionality.

Add this statement to the file.

finishsav

When you exit MATLAB, the function creates a matlab.mat in your userpath folder.

Use the MATLAB sample file finishdlg.m to display a custom dialog box allowing you to cancel when MATLAB quits.

If there is a finish.m file in your userpath folder, open it.

edit(fullfile(userpath,'finish.m'))

Otherwise, create a function named finish with no inputs or outputs and document the functionality.

Add this statement to the file.

finishdlg

To display this confirmation dialog box, set the Confirm before exiting MATLAB option in the General Confirmation Dialogs Preferences page of the Preferences.

Are you sure you want to exit MATLAB? dialog box with OK and Cancel buttons

Tips

  • When using graphics objects in finish.m, call uiwait, waitfor, or drawnow so that figures are visible.

Version History

Introduced before R2006a

See Also

| |