- On MATLAB desktop, open the "Command History" panel.
- At the top right of the "Command History" panel, select the three vertical dots.
- On the dropdown menu, click Select All.
- The selected commands can now be copied and pasted elsewhere.
How can I access the saved command history in MATLAB?
156 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2009 年 6 月 27 日
編集済み: MathWorks Support Team
2025 年 11 月 24 日 21:05
I have a number of commands appearing in my Command History window, which I would like to extract for other uses. How can I extract all of the commands from my command history?
採用された回答
MathWorks Support Team
2025 年 11 月 17 日 0:00
編集済み: MathWorks Support Team
2025 年 11 月 24 日 21:05
MATLAB R2024b and Before
Prior to MATLAB R2014a, the "history.m" file in the preference directory stores all the commands from the command history. Starting in 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 "HxSearch" tool is a MATLAB file exchange submission which users have found helpful to search for terms in the MATLAB command history.
Please refer to the "HxSearch" file exchange submission to learn more.
MATLAB R2025a and After
MATLAB R2025a no longer uses a "history.xml" file. This file still exists in the preference directory, but it is not updated by MATLAB. To extract all of the commands from the command history, please follow these steps:
It is currently not possible to extract commands which do not appear in the "Command History" window.
0 件のコメント
その他の回答 (1 件)
Adam Danz
2015 年 4 月 18 日
編集済み: MathWorks Support Team
2023 年 5 月 15 日
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:
1 件のコメント
dpb
2023 年 7 月 10 日
編集済み: dpb
2023 年 7 月 11 日
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), '>', '>');
mathist = strrep(mathist, '<', '<');
% 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...
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!