How to reopen MATLAB scripts/function when the editor was accidentally closed?
조회 수: 109 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2019년 6월 12일
편집: MathWorks Support Team
2023년 11월 22일
How to reopen MATLAB scripts/function when the editor was accidentally closed?
채택된 답변
MathWorks Support Team
2023년 11월 22일
편집: MathWorks Support Team
2023년 11월 22일
Right now there are only workarounds for this functionality, because the development team is still investigating possible implementations for this feature.
If using R2020b release or earlier of MATLAB
Trying to find a solution, you could save all open files in a mat-file and reopen these after you close this. The issue is, that you have to pro-actively save the editor state.
If you did not do this, there is the following solution which uses a XML file in the MATLAB preference directory.
In this XML file the previous state of the MATLAB Editor was saved, so you can reopen all the closed files.
Please add the code to a Favorite in MATLAB, so you have a shortcut which can be easily used.
If you do not know how to add a Favorite, please read through: https://www.mathworks.com/help/releases/R2021a/matlab/matlab_env/create-matlab-favorites-to-rerun-commands.html
Here is the code which can be pasted into a Favorite:
%parse XML file
xmlFiles = xmlread([prefdir filesep 'MATLABDesktop.xml.prev']);
%Retrieve the "clients"
FileNodes = xmlFiles.getElementsByTagName('Client');
%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('Title'));
%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{:});
If using R2021a release or later of MATLAB
In newer releases of MATLAB, you will observe another XML file named "MATLAB_Editor_State.xml" in the "filesep" preferences directory. The code for these releases of MATLAB would be as follows:
%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{:});
댓글 수: 2
추가 답변 (1개)
Josster
2023년 10월 16일
편집: MathWorks Support Team
2023년 10월 18일
Right now there are only workarounds for this functionality, because the development team is still investigating possible implementations for this feature.
If using R2020b release or earlier of MATLAB
Trying to find a solution, you could save all open files in a mat-file and reopen these after you close this. The issue is, that you have to pro-actively save the editor state.
If you did not do this, there is the following solution which uses a XML file in the MATLAB preference directory.
In this XML file the previous state of the MATLAB Editor was saved, so you can reopen all the closed files.
Please add the code to a Favorite in MATLAB, so you have a shortcut which can be easily used.
If you do not know how to add a Favorite, please read through: https://www.mathworks.com/help/releases/R2021a/matlab/matlab_env/create-matlab-favorites-to-rerun-commands.html
Here is the code which can be pasted into a Favorite:
%parse XML file
xmlFiles = xmlread([prefdir filesep 'MATLABDesktop.xml.prev']);
%Retrieve the "clients"
FileNodes = xmlFiles.getElementsByTagName('Client');
%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('Title'));
%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{:});
If using R2021a release or later of MATLAB
In newer releases of MATLAB, you will observe another XML file named "MATLAB_Editor_State.xml" in the "filesep" preferences directory. The code for these releases of MATLAB would be as follows:
%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{:});
댓글 수: 2
Haris K.
2021년 6월 27일
편집: Haris K.
2021년 6월 27일
For a user who has 70 scripts open, you sir, just saved me! Thank you both MathWorks Support and Josster!
Let me add that I was afraid to open a new script, just in case it would modify the xml and I wouldn't be able to retrieve my scripts. So I added Josster's code into a Favorite, just like it is indicated above, and then I run the code using the favourite button. It re-openned 71 tabs, missing only 5-6 from what I had open, before accidentally closing them. My guess is that it re-opens whatever you had open before your last shut-down of MATLAB.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!