File Exchange

image thumbnail

Save Entire Command History

version (1.27 KB) by Jonathan Snell
Maintains a complete record of the command history (Matlab only maintains the last 20k worth ...

1 Download

Updated 07 Dec 2005

No License

'preserve_history.m' maintains a complete record of the command history (MATLAB only maintains the last 20k worth of commands, so old commands are lost).

To get it to run you need to create a copy of your 'history.m' file and rename it 'history_saved.m'. The path of the history file will be something like "C:\Documents and Settings\poi\Application

There are paths you will need to change in 'preserve_history.m' depending on your installation directory and username on lines: 9,25,47.

Also attached are 'startup.m' and 'finish files.m' which call the preserve_history file and also restore MATLAB to the last working directory whenever it is started.

The attached files must all be copied to "...matlab_directory\toolbox\local\".

Cite As

Jonathan Snell (2020). Save Entire Command History (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)


Still works on R2013a A few notes for those who are trying this- if you have other commands that have altered startup.m (like ezyfit) you have to seek those out and ensure that you have merged the startup.m files to accomplish all the startup tasks required (for instance, ezyfit, as well as the preserve_history functions).

I found my altered startup.m file in my "userpath" - try that on the command line to see what yours is. Matlab suggests using this path for startup.m as well as other user mfiles necessary for your work for easy path inclusion.

Note that any other startup.m files that are higher up in the path (in the path dialog in matlab) will be executed first, and all subsequent startup.m files will be ignored.

Utilizing these files and matlab's own startupsav.m file (located in toolbox/local) I now have a program that launches from where I left off before and pre-loads any variables that I was working with from a default matlab.mat file in the directory that I was working in previously.

I love it! Thanks!!



Great file, used this to work with matlab -nodesktop option, which has a quite limited historic x)

Jon C

Mr Smart

Thanks for this great contribution! I was looking for this functionality, and it works great!

@Eric I got around the error problem in finish.m by placing the history_preserved command in a try cath block. So finish.m will look like:

catch EM
h=msgbox(EM.message,sprintf('Error: %s',EM.identifier),'error');

Now it won't 'hang' your matlab but just display an error dialog before closing.





I'm not sure why Matlab doesn't allow the command history size to be adjusted, but this is a must have.

I made a string variable "rootpath" (e.g. 'c:\users\poi\AppData\*etc*') and then used [rootpath,'history*.m'] on the three lines that need to be edited, I think this is an obvious change that I assume many people would use.

Thanks for the program. I was moving command histories from one computer to another; giving write permissions, and copying the old history to the new one plus history_save was a little difficult, but most competent users should be able to overcome this.

One word of caution: if there is an error in "history_preserved.m" then you'll have to crash matlab (from the task manager in Windows) to get it to restart because finish.m will keep returning an error and prevent the program from closing.

Mason Freed

I'd post code if I could, but to make the preserve_history.m file not require customization, and work for multiple Matlab installations, just change lines 13, 29, and 51 to these:

13: [fid, err] = fopen(fullfile(prefdir,'history_saved.m'), 'r');
29: [fid, err] = fopen(fullfile(prefdir,'history.m'), 'r');
51: [fid, err] = fopen(fullfile(prefdir,'history_saved.m'), 'r+');

James Blake

It's a bit tricky to install---you need to modify preserve_history.m in several places. There should be a parameter at the top of the file to make it easier, unless you can automate it further. Adding finish.m and preserve_history.m to your path, rather than touching the matlab install files is important, since normal users can do this without disrupting any other usage. It could also do with some clearer help. All in all, it works well, though.

Yair Altman

Excellent & simple implementation fixing a long-standing Matlab annoyance. The preserve_history file could use extra help comment about the usage. Also, should use the built-in prefdir built-in function so that users won't need to modify the file. Lastly, the feature of restoring the last used folder should be placed in a separate submission - it is irrelevant in this one. All this said, this is still a great submission.

Scott Freeman

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Save entire command history - Updated, Fast