How do I import the command history from one version of MATLAB to another?

27 views (last 30 days)
I have just begun using a new version of MATLAB, but do not have access to my old command history. I would like to import it from my old version into my newer one. How can I do this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Jul 2016
Edited: MathWorks Support Team on 13 Jul 2016
Since MATLAB R2014a, the command history is saved in “history.xml” file and in the versions prior to R2014a, the command history is saved in the “history.m” file. This is because the new command history makes use of other information, such as number of times a command was repeated, whether it resulted in an error, and the execution time. This information is all stored as attributes in the XML elements for "commands".
1. If you are importing command history between two releases of MATLAB that use “history.m”, i.e., releases of MATLAB prior to MATLAB R2014a, please look at the following example : 
To import command history from Release 2007b (R2007b) to Release 2009b (R2009b), use the following steps:
 
           a. In R2007b, execute the PREFDIR command to find the path location of the preference directory.
           b. Navigate to that folder and copy the file named "history.m"
           c. Go up one level and then into the equivalent 'R2009b' preference directory. Paste the 'history.m' file there.
           d. Restart R2009b if you have it open. The new command history will be displayed in the Command History pane.
 
2. If you are trying to import command history between two releases of MATLAB after R2014a, please follow the steps 1-a to 1-d but copy the file “History.xml” instead of “history.m”  
3. If you are trying to import command history between the two releases of MATLAB where one of the release uses “history.m” to save command history and the other release uses “History.xml”, you will have to convert the “history.m” to “History.xml”. To do this, you will need to read in your old history file line by line and write these into appropriate structure tags of an XML file. In this case, you only need the <history> tag and each subsequent command in a child <command> tag. It is fine if you do not provide additional attribute information, since your old history does not have this. They will still be parsed properly by MATLAB. In order to have import your old history from history.m into a new History.xml file, the following code will read in an existing history.m file and write the corresponding History.xml file:
Important Note: Do not run the following script in your MATLAB preference directory as it may corrupt your current 'history.xml' file.
 
fid = fopen('history.m');
% Create the document node and the root element, 'history'
docNode = com.mathworks.xml.XMLUtils.createDocument('history');
% Get the history node
history = docNode.getDocumentElement;
% Read the first line of the old history.m file
line = fgetl(fid);
% Loop through every line of the old history.m file while there are lines
while ischar(line)
% Add a command element for each command
command = docNode.createElement('command');
% Add the command itself as the child text node
command.appendChild(docNode.createTextNode(line));
% Add the command node back as a child of the command node
history.appendChild(command);
% Get the next line of the old history.m file
line = fgetl(fid);
end
% Close the file
fclose(fid);
% Write the XML file
xmlwrite('History.xml',docNode);
 
 
To use this new XML History file, simply place it into your Preferences directory (found by executing the PREFDIR command). To avoid overwriting an existing History.xml file, you can rename the file to History_old.xml.
 
After restarting MATLAB, the new History.xml file will be loaded for your pop-up command history.
You will notice that there are not any details about execution time, the session in which you used the command, etc., because this information was never provided in the XML file. However, as you enter new commands, your History.xml file will create a new session with the date, and each subsequent command will now have any associated attributes (such as execution time).
 
  1 Comment
Peter Muellers
Peter Muellers on 13 Jul 2016
IMPORTANT: Don't run the above script with the MATLAB preferences directory (prefdir) as the current directory. Instead run it elsewhere and copy the History.xml file it produces into the prefdir. Running the script within the prefdir may result in a corrupt History.xml, as both the script and the MATLAB history writing thread contend for access to the file.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!