Main Content

Command History

View previously run statements

Description

The Command History displays a log of statements that you ran in the current and previous MATLAB® sessions. The Command History lists the date and time of each session in the short date format for your operating system, followed by the statements from that session. Brackets in the left margin indicate statements that were processed as a group. A red mark precedes each group of statements that generated an error.

You can select entries in the Command History and then perform these actions for the selected entries.

Action

How to Perform the Action

Create a script from statements.

Select an entry or entries, and then right-click and select Create Script or Create Live Script from the context menu. The Editor or Live Editor opens a new file that contains the statements you selected from the Command History.

Rerun previous statements.

Do one of the following:

  • Press the up arrow key (↑) until the statement you want appears at the prompt, and then press Enter.

  • Double-click an entry in the Command History.

  • Select an entry or entries in the Command History and press Enter.

To extend the selection to include multiple statements, press Shift+↑.

Copy statements to another window.

Select an entry or entries, and then do one of the following:

  • Right-click and select Copy from the context menu. Paste the selection into an open file in the Editor or any application.

  • From the Command History panel, drag the selection to an open file or another application.

Create a favorite command from statements.

Select an entry or entries, and then right-click and select Create New Favorite. The Favorite Commands Editor opens with the selected entries added to the Code field. Configure the other fields and click Save.

For more information about favorite commands, see Rerun Favorite Commands.

Delete entries.

Select the entries to delete, and then right-click and select Delete, or press the Delete key.

To delete all entries, click the actions button and select Clear Command History from the context menu. You cannot recall entries deleted in this way.

Command History showing a list of previously run statements

Open the Command History

You can view the Command History in a pop-up window off of the Command Window or docked in the Command History panel.

  • Pop-up window – To view all the history, press the up arrow key (↑) in the Command Window. To view a specific statement, type any part of the statement at the prompt and then press the up arrow key.

    The Command History pop-up window closes after you act on a selected statement. You also can close the pop-up window by pressing the Esc key. To disable the pop-up window, go to the Home tab, and in the Environment section, click Layout. Then, in the Layout Options section, clear the Command History > Popup option.

  • Docked – Click the Command History icon on the sidebar. If the Command History icon is not on a sidebar, click the Open more panels button and select Command History. You also can enter commandhistory in the Command Window.

To change how MATLAB searches for results in the Command History, click the actions button in the Command History pop-up window or panel and select from the available options, including Match Anywhere, Match Beginning, and Match Case. For more information about additional settings, see Modify Command History Settings.

Examples

expand all

Use the Command History to find and run previous plot statements.

In the Command Window, run these statements to create two line plots.

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

x1 = linspace(-2*pi,2*pi);
y1 = sin(x1);
y2 = cos(x1);

figure
plot(x1,y1,x1,y2)

Type plot and then press the up arrow key. MATLAB displays the Command History with all instances of plot highlighted. The last run instance of plot is selected.

Command History showing a list of previously run commands and two instances of the word plot highlighted in yellow

Press the up arrow key to select the previous run instance of plot and press Enter. MATLAB runs the statement plot(x,y).

Command History showing a list of previously run commands and two instances of the word plot highlighted in yellow. The first instance of plot is selected.

Programmatic Use

expand all

commandhistory opens the Command History panel when it is closed, and selects the Command History panel when it is open.

More About

expand all

Version History

Introduced before R2006a

expand all