How can I access the saved command history in MATLAB? (2024)

236 views (last 30 days)

Show older comments

MathWorks Support Team on 27 Jun 2009

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab

Edited: dpb on 11 Jul 2023

Accepted Answer: MathWorks Support Team

I have a number of commands appearing in my Command History window, which I would like to extract for other uses.

Sign in to answer this question.

Accepted Answer

MathWorks Support Team on 15 May 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab#answer_106853

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab#answer_106853

Edited: MathWorks Support Team on 15 May 2023

Prior to MATLAB R2014a the "history.m" file in the preference directory stores all the commands from the command history. Starting with MATLAB R2014a the commands are stored in an XML file history.xml in the preference directory. The location of the directory can be found by executing the following on

MATLAB command prompt.

>>prefdir

The following is a MATLAB file exchange submission which users may found useful to search for terms in the MATLAB command history.https://www.mathworks.com/matlabcentral/fileexchange/50551-hxsearch

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (1)

Adam Danz on 18 Apr 2015

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab#answer_175763

Edited: MathWorks Support Team on 15 May 2023

I was also searching for a solution to a similar problem and wanted to note my solution here in case others are still looking.

Matlab versions R2014a and later store command history in history.xml while earlier versions store command history in history.m in prefdir. My work stations have earlier versions while my home desktop has later version. So, I wrote a function that searches command history no matter what version of matlab is installed. This function extracts commands in the form of character strings as described in the question above.

If anyone else is interested:

https://www.mathworks.com/matlabcentral/fileexchange/50551-hxsearch

1 Comment

Show -1 older commentsHide -1 older comments

dpb on 10 Jul 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab#comment_2811093

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/97504-how-can-i-access-the-saved-command-history-in-matlab#comment_2811093

Edited: dpb on 11 Jul 2023

Open in MATLAB Online

I made mod to be able to open an arbitrary history file -- the desktop died and had to keep going on a time-critical piece of work so moved stuff from backups/old drive to laptop, including the history file. For expediency, I just copied it into my cwd and pointed prefsdir to it...using Case 2 to do so...

....

case 2 % specific history file instead -- looks in cwd at moment

%search History.xml (this conditional section inserted by dpb )

prefdir=cd; % look in current directory first

hpath = fullfile(prefdir,'History.xml');

if exist(hpath,'file')

% Read the XML file into a string to look as the older history.m (i.e. remove all XML tags)

mathist = fileread(hpath);

mathist = regexprep(mathist, '(<[^>]+>\s*)+', '$$**@@', 'lineanchors');% '$$**@@' arbitrarily chosen to mark end of each line for later parsing

% translate html entities and remove leading newline

mathist = strrep(mathist(7:end), '&gt;', '>');

mathist = strrep(mathist, '&lt;', '<');

% replace \r and \r\n with \n (safe to copy between OSes)

mathist = regexprep(mathist, '\r(\n)?', '\n');

% parse mathist into cell

mathist = strsplit(mathist, '$$**@@');

else

disp ('History.xml not found. No history in current working directory.')

disp ('Not yet generalized to open any history.xml file location.')

return

end

end

Had to adjust the check on allowable inputs for session, too, of course...

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABProgrammingFiles and FoldersFile Operations

Find more on File Operations in Help Center and File Exchange

Tags

No tags entered yet.

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How can I access the saved command history in MATLAB? (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How can I access the saved command history in MATLAB? (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5643

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.