How to reopen MATLAB scripts/function when the editor was accidentally closed?
121 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 12 Juin 2019
Modifié(e) : MathWorks Support Team
le 22 Nov 2023
How to reopen MATLAB scripts/function when the editor was accidentally closed?
Réponse acceptée
MathWorks Support Team
le 22 Nov 2023
Modifié(e) : MathWorks Support Team
le 22 Nov 2023
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 commentaires
Plus de réponses (1)
Josster
le 16 Oct 2023
Modifié(e) : MathWorks Support Team
le 18 Oct 2023
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 commentaires
Haris K.
le 27 Juin 2021
Modifié(e) : Haris K.
le 27 Juin 2021
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.
Voir également
Catégories
En savoir plus sur Code Execution dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!