Eghbal
Eghbal

Reputation: 3783

History of previously opened m-files in MATLAB

Is anyway to find history of previously opened m-files in MATLAB R2014b from 2 or 3 months ago? (a list of name of files and paths)

Upvotes: 5

Views: 9072

Answers (4)

CAta.RAy
CAta.RAy

Reputation: 514

Base in the answer by CitizenInsane, but for any Matlab version.

To find the .xml file in any Matlab version, use prefdir:

>> prefdir

ans =  '/Users/user/Library/Application Support/MathWorks/MATLAB/R2018a'

MATLAB_Editor_State.xml will be stored there. Therefore the fuction would be:

function [recentFiles] = GetRecentFiles()

% Opens editor's state file
filepart = sprintf([ prefdir '/MATLAB_Editor_State.xml']);
filename = fullfile(getenv('APPDATA'), filepart);
document = xmlread(filename);

% Get information about 'File' nodes
recentFiles = struct([]);
fileNodes = document.getElementsByTagName('File');
for fni = 1:(fileNodes.getLength())

   attributes = fileNodes.item(fni-1).getAttributes(); % Careful, zero based indexing !

   for ai = 1:(attributes.getLength())

       % Get node attribute
       name = char(attributes.item(ai-1).getName()); % Zero based + need marshaling COM 'string' type
       value = char(attributes.item(ai-1).getValue()); % Zero based + need marshaling COM 'string' type

       % Save in structure
       name(1) = upper(name(1)); % Just because I prefer capital letter for field names ...
       recentFiles(fni).(name) = value;

   end

end    

Upvotes: 2

Matt Gaidica
Matt Gaidica

Reputation: 13

In R2018b you can increase the Most recently used file list in Preferences > Editor/Debugger. I like the methods above, but they do not work if you're working across machines (e.g., using Github). I coded a solution that uses the modified file date from the machine, instead of relying on MATLAB itself.

Upvotes: 0

Pablo Nunes
Pablo Nunes

Reputation: 21

Last answer waIs really helpful. I've just modified it to read and open the recent tab files. This works on Matlab R2013a:

function [recentFiles] = recover_tabs()
%[
% Opens editor's state file
filepart = sprintf('MathWorks\\MATLAB\\R%s\\%s', version('-release'), 'MATLAB_Editor_State.xml');
filename = fullfile(getenv('APPDATA'), filepart);
document = xmlread(filename);

% Get information about 'File' nodes
recentFiles = struct([]);
fileNodes = document.getElementsByTagName('File');
for fni = 1:(fileNodes.getLength())

   attributes = fileNodes.item(fni-1).getAttributes(); % Careful, zero based indexing !

   for ai = 1:(attributes.getLength())

       % Get node attribute
       name = char(attributes.item(ai-1).getName()); % Zero based + need marshaling COM 'string' type
       value = char(attributes.item(ai-1).getValue()); % Zero based + need marshaling COM 'string' type

       % Save in structure
       name(1) = upper(name(1)); % Just because I prefer capital letter for field names ...
       recentFiles(fni).(name) = value;

   end

end    

%     loop to access files in the tab history
for j=1:length(recentFiles)
    arquivo = [recentFiles(j).AbsPath '\' recentFiles(j).Name];
%         if exists, then open
    if exist(arquivo, 'file') == 2
        open(arquivo);
    end
end

%]
end

Upvotes: 2

CitizenInsane
CitizenInsane

Reputation: 4855

Matlab R2014b stores its recent files in:

%APPDATA%\MathWorks\MATLAB\R2014b\MATLAB_Editor_State.xml

It's a .xml file so it's easy to load and parse with xmlread. I'm not very familiar with xml parsing syntax, but here is how to get information about files (to be adapted to your needs of course):

function [recentFiles] = GetRecentFiles()
%[
    % Opens editor's state file
    filepart = sprintf('MathWorks\\MATLAB\\R%s\\%s', version('-release'), 'MATLAB_Editor_State.xml');
    filename = fullfile(getenv('APPDATA'), filepart);
    document = xmlread(filename);

    % Get information about 'File' nodes
    recentFiles = struct([]);
    fileNodes = document.getElementsByTagName('File');
    for fni = 1:(fileNodes.getLength())

       attributes = fileNodes.item(fni-1).getAttributes(); % Careful, zero based indexing !

       for ai = 1:(attributes.getLength())

           % Get node attribute
           name = char(attributes.item(ai-1).getName()); % Zero based + need marshaling COM 'string' type
           value = char(attributes.item(ai-1).getValue()); % Zero based + need marshaling COM 'string' type

           % Save in structure
           name(1) = upper(name(1)); % Just because I prefer capital letter for field names ...
           recentFiles(fni).(name) = value;

       end

    end    
%]
end

This returns a structure like this:

recentFiles = 

1x43 struct array with fields:

    AbsPath
    LastWrittenTime
    Name

NB: I've tried to type in matlab command window matlab.desktop.editor.*, but seems there's nothing regarding recent files (anyway there are a lot of interesting things to manipulate the editor from the command line)

Upvotes: 12

Related Questions