access to matlab history while running in nodesktop mode

8 views (last 30 days)
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.

Answers (2)

Walter Roberson
Walter Roberson on 3 May 2016
Edited: Walter Roberson on 3 May 2016
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 Comments
Leo Simon
Leo Simon on 3 May 2016
Thanks Walter
unfortunately, in no desktop mode it returns
java.lang.String[]:
[0 element array]
Walter Roberson
Walter Roberson on 4 May 2016
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)

Sign in to comment.


Anthony Barone
Anthony Barone on 8 May 2018
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

Categories

Find more on Code Execution in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!