MATLAB Answers

How can I save a list of open m-files?

53 views (last 30 days)
Is there a function that returns the list of files currently open in the editor?
I frequently work with 25+ m-files open. When quitting and restarting MATLAB, the open files are reopened as desired (preferences setting). However, in the rare case that MATLAB crashes, I have often restarted to find an empty editor. It is a pain to manually select and reopen each m-file. If there is a function for getting the list, I can run it in my finish.m script and save the results. That way I always have a way to reload open files.

Accepted Answer

Adam Schuyler
Adam Schuyler on 21 Sep 2011
I found a way. It's not pretty, but in case someone else might find this useful...
In my user folder (on OSX) there is .matlab/R2009a/MATLABDesktop.xml
There is a LOT of stuff in this file, but I found many entries of the form:
<ClientData EditorFileName="FULL PATH TO ONE OF MY OPEN M-FILES"/>
All my m-files are at /Volumes/data/adam/...
The following grep commands find the desired lines in the xml file and extract JUST the m-file paths:
grep 'EditorFileName' MATLABDesktop.xml | grep -o '\"/Volumes/.*\.m\"'
Copy/paste the list into a cell array and
edit(X{:})
The API referenced by Sean de seems like a much nicer option for those using newer releases.

More Answers (6)

Jan
Jan on 21 Sep 2011
The editor API has not changed, it is only documented now.
ES = com.mathworks.mlservices.MLEditorServices;
OpenFiles = ES.builtinGetOpenDocumentNames;
C = cell(1, numel(OpenFiles));
for i = 1:length(C)
C{i} = char(OpenFiles(i));
end
  2 Comments
Jan
Jan on 11 Nov 2017
With modern Matlab versions:
docs = matlab.editor.getAll;
Name = {docs.Filename};

Sign in to comment.


Peter O'Connor
Peter O'Connor on 2 Mar 2012
Yep, at least on R2011A there is.
f=matlab.desktop.editor.getAll;
If you just want the filenames, then go
filenames=cellfun(@(s)s(find(s==filesep,1,'last')+1:end-2),{f.Filename},'uniformoutput',false);
  1 Comment
Dan
Dan on 4 Jun 2013
Peter,
I can't kind help on this call. Is there a way to restore the list?

Sign in to comment.


Sean de Wolski
Sean de Wolski on 21 Sep 2011
See this question and answers from last week:
  1 Comment
Adam Schuyler
Adam Schuyler on 21 Sep 2011
Thanks for the quick reply Sean de, but I'm using R2009a and R2010a, so the editor API is not available to me. I captured the open m-file list once before (1+ year ago?), so I know it is possible. I really wish I saved the function call...

Sign in to comment.


MichaelR
MichaelR on 19 Sep 2015
My suggestion to backup the list of open files on a mac:
- Exit Matlab - Backup from .matlab/R20xxy both MATLABDesktop.xml and MATLABDesktop.xml.prev for instance by copying them to the desktop - Launch Matlab and do your stuff
To revert to the former files copy both files back to .matlab/R20xxy
Rmk: .matlab is hidden under your root directory. It is hidden because one should not tinker with those files :). Rmk: matlab updates the two files regularly, so probably it is a good idea to do the file manipulations with matlab shut down Rmk3: Seems to work for versions since at least R2012b on

alieed
alieed on 4 Jan 2020
Try this
% creat list of your m-files
clear
clc
openFiles = matlab.desktop.editor.getAll;
mfileNames = {openFiles.Filename};
save (['mfileNames' date],'mfileNames')
f=char(mfileNames(35))
% reload them in the future whenever you want
load('mfileNames04-Jan-2020.mat')
for i=1:length(mfileNames)
A=open (char(mfileNames(i)))
end
% All the best :)@ ali2020
  2 Comments
Michel Bertrand
Michel Bertrand on 5 Dec 2020
oops.... it does work with R2020b, and I could make it work with R2019b.

Sign in to comment.


Steven Lord
Steven Lord on 5 Dec 2020
This wasn't an option when the question was asked originally, but you may want to create a Project to manage your files.

Community Treasure Hunt

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

Start Hunting!

Translated by