access to matlab history while running in nodesktop mode
8 次查看(过去 30 天)
显示 更早的评论
I'd like to have access to a file that has saved my command history while running in nodesktop mode. This question was asked in 2011 in this thread. Basically the answer by Walter Roberson was negative: the only option was to scroll up and down, and cut and paste. I was hoping that since 2011 maybe Mathworks had provided a better option. If there really isn't a solution, is it possible to open matlab in desktop mode with only the history window showing?
Thanks very much for any suggestions.
0 个评论
回答(2 个)
Walter Roberson
2016-5-3
编辑:Walter Roberson
2016-5-3
With sufficiently new versions (I do not know which release), see http://www.mathworks.com/matlabcentral/fileexchange/50551-hxsearch
which implicitly suggests,
com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory
which returns a cell array of strings.
Though I am not certain that this would work in nodesktopmode .
2 个评论
Walter Roberson
2016-5-4
The above contribution shows reading in from the history.xml file which is probably what you would have to do to access an existing history (as opposed to history for the current session, which is not kept if you are in nodesktop mode)
Anthony Barone
2018-5-8
This question is old and the original poster has probably long since mved on, but I figured in case anyone else comes across this looking for the ability to access past commands from nodesktop mode (like I did a few days ago) they will actually have an answer.
NOTE: this is valid for MATLAB R2017b, but i cant find this functionality listed in any documentation, so idk if this will be the case in other version of MATLAB.
Basically, The "main" history is kept in
fullfile(prefdir,'History.xml')
It turns out that, when running in nodesktop mode, MATLAB keeps a 2nd history in
fullfile(prefdir,'history.m')
These histories are mutually exclusive: history.m ONLY records nodesktop mode commands, and History.xml only records commands made with the full desktop environment active.
I dont know of any builtin functions to access the nodesktop mode's history.m, but its easy to get previous commands using 'fopen' and 'fgetl'.
On the machine I use, both of these history files are located in
${HOME}/.matlab/R20*/
Personally, I use something like the following:
function [outStr] = getLastCommandStr
%%grabs the last command entered using one of 2 histories
if usejava('desktop')
outStr = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
outStr = char(outStr(end));
return
else
outStr = [''];
if exist(fullfile(prefdir,'history.m'),'file')
fid = fopen(fullfile(prefdir,'history.m'),'r');
hType = 1;
elseif exist(fullfile(prefdir,'History.xml'),'file')
fid = fopen(fullfile(prefdir,'History.xml'),'r');
hType = 2;
else
return
end
lineRecord = cell(5,1);
lineRecord{1} = fgetl(fid);
while ~feof(fid)
lineRecord = lineRecord([5,1:4]);
lineRecord{1} = fgetl(fid);
end
fclose(fid);
callerName = getCallerName;
if hType == 2
for nn = [1:numel(lineRecord)]
parseTest=regexprep(lineRecord{nn},['^<command .*>(.*',callerName,'.*)</command>$'],'$1');
if ~strcmp(lineRecord{nn},parseTest)
outStr = parseTest;
return
end
end
elseif hType == 1
for nn = [1:numel(lineRecord)]
if ~isempty(regexp(lineRecord{nn},['^.*',callerName,'.*$'],'once'))
outStr = lineRecord{nn};
return
end
end
end
end
end
function [callerFunc] = getCallerName(fullStackFlag)
%%Return the name of the calling function.
%
% 'fullStackFlag': instead return a cellstr array with the name of every function in the stack. DEFAULT: false.
%
% IF the function that called 'getCallerName' called from the base workspace, 'base' is returned.
% IF this function is called directly from the base workspace, an empty char vector (or empty cell array) is returned.
%
% NOTE: This returns the name of whatever called the function that called 'getCallerName', not the immediate caller.
% I.E., running 'getCallerName' from within a function doesnt return the functions name, it returns the name of whatever called it.
% To get the name of the immediate caller (i.e., the current function, not the calling function), use 'mfilename' instead.
d = dbstack;
if ~exist('fullStackFlag','var') || isempty(fullStackFlag) || ~(islogical(fullStackFlag) || isnumeric(fullStackFlag)) || fullStackFlag == 0
if numel(d) == 1
callerFunc = '';
elseif numel(d) == 2
callerFunc = 'base';
else
callerFunc = d(3).name;
end
else
callerFunc = cell(numel(d)-1,1);
[callerFunc{:}] = deal(d(2:end).name);
end
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!