How to Save Command Window Contents in MATLAB

Ammar Ali Feb 02, 2024
How to Save Command Window Contents in MATLAB

This tutorial will discuss how to save the command window contents using the diary command in MATLAB.

Save Command Window Contents Using the diary Command in MATLAB

To save the contents of the command window in MATLAB, you can use the diary command. The diary command saves the contents of the command window in a specified file; if no file is specified, a file name diary will be used. To save the contents, first, you need to create a diary at the start of your MATLAB session using the diary command, and when you are done with your session, you have to turn the diary off using the diary off command. For example, let’s save some command window contents to a text file. See the command window code below.

>> diary('fileName.txt')
>> a = 1:10

a =

     1     2     3     4     5     6     7     8     9    10

>> b = 11:20

b =

    11    12    13    14    15    16    17    18    19    20

>> c = a+b

c =

    12    14    16    18    20    22    24    26    28    30

>> diary('off');

Output:

save command window content using diary command in matlab

As you can see the contents of the command window are saved inside a text file. Note that the diary command will append the data to the previous data if it’s present, which means the size of the file will increase, so it’s a better practice to close the diary when you are finished. If you want to save the contents to a new file, you can specify a new name.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook