This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Terminate MATLAB program


quit cancel
quit force


quit displays a confirmation dialog box if the confirm that upon quitting preference is selected, and if confirmed or if the confirmation preference is not selected, terminates MATLAB® after running finish.m, if finish.m exists. Call quit from the MATLAB command prompt. To interrupt a MATLAB command, see Stop Execution.

The quit function does not automatically save the workspace. To save the workspace or perform other actions when quitting, create a finish.m file to perform those actions. For example, you can display a custom dialog box to confirm quitting using a finish.m file—see the following examples for details. If an error occurs while finish.m is running, quit is canceled so that you can correct your finish.m file without losing your workspace.

quit cancel is for use in finish.m and cancels quitting. It has no effect anywhere else.

quit force bypasses finish.m and terminates MATLAB. Use this syntax to override finish.m, for example, if an errant finish.m does not let you quit.


MATLAB includes two sample finish.m files. Use them to help you create your own finish.m, or rename one of the files to finish.m to use it.

  • finishsav.m—Saves the workspace to a MAT-file when MATLAB quits.

  • finishdlg.m—Displays a dialog box allowing you to cancel quitting; it uses quit cancel and contains the following code:

    button = questdlg('Ready to quit?','Exit Dialog','Yes','No','No');
    switch button
        case 'Yes',
            disp('Exiting MATLAB')
            % Save variables to matlab.mat
        case 'No',
            quit cancel;


When using graphics objects in finish.m, use uiwait, waitfor, or drawnow so that figures are visible. See the reference pages for these functions for more information.

If you want MATLAB to display the following confirmation dialog box after running quit, select Preferences in the Environment section on the Home tab. Then select the check box for Confirm before exiting MATLAB, and click OK.


As an alternative to the quit function, use the Close box in the MATLAB desktop.

Introduced before R2006a

Was this topic helpful?