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

조회 수: 57 (최근 30일)
Adam Schuyler
Adam Schuyler 2011년 9월 21일
답변: Souarv De 2023년 10월 12일
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.

채택된 답변

Adam Schuyler
Adam Schuyler 2011년 9월 21일
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.
  댓글 수: 1
Sam Zebrado
Sam Zebrado 2021년 11월 16일
Love this solution: it seems to be able to save the layout as well

댓글을 달려면 로그인하십시오.

추가 답변 (8개)

Kurt von Niederhausen
Kurt von Niederhausen 2021년 4월 22일
function saveListOfOpenFiles(listFile)
fid = fopen(listFile,'w');
f=matlab.desktop.editor.getAll;
% filenames=cellfun(@(s)s(find(s==filesep,1,'last')+1:end-2),{f.Filename},'uniformoutput',false);
filenames=cellfun(@(s)s,{f.Filename},'uniformoutput',false);
for(i = 1:length(filenames))
fname = filenames{i};
fprintf(fid, '%s \n', fname);
end
fclose(fid);
end
function openFilesInList(listFile)
fid = fopen(listFile);
while(~feof(fid))
file = fgetl(fid);
fprintf('Opending File: %s \n', file);
edit(file);
end
fclose(fid);
end
I created two Matlab Functions: saveListOfOpenFiles and openFilesInList. Now you can just call this at any time you have the editor the way you want and then retrieve the open files with the commands:
saveListOfOpenFiles('List_Open_Files.txt')
openFilesInList('List_Open_FIles.txt')
  댓글 수: 1
DEEPAK BHORIYA
DEEPAK BHORIYA 2021년 11월 9일
Thanks man!
The scripts are really helpful.

댓글을 달려면 로그인하십시오.


Jan
Jan 2011년 9월 21일
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
  댓글 수: 3
Jan
Jan 2017년 11월 11일
With modern Matlab versions:
docs = matlab.editor.getAll;
Name = {docs.Filename};
Andrew Sandeman
Andrew Sandeman 2022년 8월 29일
*typo: matlab.desktop.editor.getAll
but otherwise works well

댓글을 달려면 로그인하십시오.


Peter O'Connor
Peter O'Connor 2012년 3월 2일
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
Dan
Dan 2013년 6월 4일
Peter,
I can't kind help on this call. Is there a way to restore the list?

댓글을 달려면 로그인하십시오.


alieed
alieed 2020년 1월 4일
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
Michel Bertrand
Michel Bertrand 2020년 12월 5일
This does not pick mlx files in the editor. I am still looking for a solution
Michel Bertrand
Michel Bertrand 2020년 12월 5일
oops.... it does work with R2020b, and I could make it work with R2019b.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2011년 9월 21일
See this question and answers from last week:
  댓글 수: 1
Adam Schuyler
Adam Schuyler 2011년 9월 21일
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...

댓글을 달려면 로그인하십시오.


MichaelR
MichaelR 2015년 9월 19일
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

Steven Lord
Steven Lord 2020년 12월 5일
This wasn't an option when the question was asked originally, but you may want to create a Project to manage your files.
  댓글 수: 1
Jim Leveque
Jim Leveque 2021년 9월 13일
I really like the way that Visual Studio Code handles this, with workspaces that you can load or close. The project idea seems very heavy-handed since it inserts MATLAB too far into the development loop. e.g., source control, which is usually done outside MATLAB.

댓글을 달려면 로그인하십시오.


Souarv De
Souarv De 2023년 10월 12일
%parse XML file
xmlFiles = xmlread([prefdir filesep 'MATLAB_Editor_State.xml']);
%Retrieve the "clients"
FileNodes = xmlFiles.getElementsByTagName('File');
%get the length of the FileNodes
nrFiles = FileNodes.getLength;
%initialize Files
Files = cell(nrFiles,1);
%initialize isFile
isFile = zeros(nrFiles,1);
%Iterate over all Elements and check if it is a file.
for iNode = 0:nrFiles-1 % Java indexing.
%Files
Files{iNode+1} = [char(FileNodes.item(iNode).getAttribute('absPath')),...
filesep,char(FileNodes.item(iNode).getAttribute('name'))];
%check if the "client" is a file:
isFile(iNode+1) = exist(Files{iNode+1},'file') == 2 && ~(strcmp(Files{iNode+1},'Workspace'));
end
%remove the other files:
MyFiles = Files(find(isFile));
%open the files in the editor:
edit(MyFiles{:});

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by