MATLAB Save Command Window Contents
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');

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.
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.
Copyright © 2023. All right reserved
How to save the contents of MATLAB’s Command Window to a file?
By «command window», you mean the textual contents of the window, or the contents of the local workspace?
Apr 29, 2011 at 14:29
1 Answer 1
You have a few options available for saving content from the Command Window:
-
You can do this using the DIARY command. You could even automate this so that it always records what you do by modifying your startup.m file to turn on text logging:
diary('myTextLog.txt'); %# Text will be appended if this file already exists
And then modify your finish.m file to turn logging off:
diary('off');
matlab -logfile "myTextLog.txt"
Is it possible to save the contents of my Command Window to PDF in MATLAB 7.7 (R2008b)?
I want to be able to save the contents of my Command Window to PDF, much like the DIARY command allows me to save to TXT.
Accepted Answer
MathWorks Support Team on 27 Jun 2009
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
To save the contents of your Command Window to PDF in MATLAB 7.7 (R2008b):
1. Undock the Command Window from the MATLAB desktop (if it is already undocked, then skip this step).
2. Navigate to File->Print
3. When the Print dialog window opens. Check the ‘Print to File’ checkbox and select Adobe PDF as the printer of choice from the drop down menu.
Saving data from the command window
I have a matlab code, where I need to save the data (as a txt file) at the end. I can save it in a file in the editor itself. Is there any way that I can save the data from the command window? I mean, once run the code, I will asked to save it or not, if say yes, then it will ask me to give a name and then save it.
I appreciate for your help. Graig.
0 Comments
Accepted Answer
Walter Roberson on 14 Feb 2011
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
Do you have the data stored in a matrix at the time you would be asking about saving, or is the data to be stored «whatever happened to get written to the command window» including by routines that you have no control over?
If you need to save «whatever appeared in the command window» then you should consider creative use of «diary».
If you have all of the data, then it becomes a matter of using questdlg() and uiputfile()
0 Comments
More Answers (2)
Graig on 14 Feb 2011
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
Thanks Walter. Ohh yeah, I have all the data in a matrix form before saving it.
0 Comments
Graig on 14 Feb 2011
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
Thanks walter, but I am confused with the uiputfile syntex. How to save variable ‘a’ in the workspace using uiputfile?
