No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Save Entire Command History

4.7 | 14 ratings Rate this file 8 Downloads (last 30 days) File Size: 1.27 KB File ID: #9291 Version: 1.0

Save Entire Command History



07 Dec 2005 (Updated )

Maintains a complete record of the command history (Matlab only maintains the last 20k worth ...

| Watch this File

File Information

'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\".


This file inspired Save Entire Command History Updated, Fast.

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
18 Feb 2014 Allen

Allen (view profile)

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!!

21 Aug 2013 Werner

Werner (view profile)

17 Aug 2013 Werner

Werner (view profile)

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

01 Feb 2013 Jon C

Jon C (view profile)

29 Nov 2010 Mr Smart

22 Sep 2010 Emile Demarteau

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.

16 Jun 2010 Jie Zhang

01 Mar 2010 mark95tt

20 Feb 2010 Eric

Eric (view profile)

20 Feb 2010 Eric

Eric (view profile)

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.

Comment only
30 Jul 2009 Nathan Thern

17 Jun 2009 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+');

31 May 2007 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.

23 May 2007 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.

08 Dec 2006 Scott Freeman

Contact us