How do I close all open scripts in the Editor window?
Show older comments
How do I close all open scripts in the Editor window?
Accepted Answer
More Answers (2)
Image Analyst
on 2 Apr 2020
Edited: Image Analyst
on 2 Apr 2020
The following snippets may be useful for closing down:
- m-file editor windows, and/or
- variable editor windows
OPTION 1:
% Close all script editor windows/tabs with no prompt.
closeNoPrompt(matlab.desktop.editor.getAll);
OPTION 2:
% Close all script editor tabs, one at a time with no prompt, and listing names of the closed files in the command window.
editorTabs = matlab.desktop.editor.getAll;
for k = 1 : length(editorTabs)
fprintf('Closing %s...\n', editorTabs(k).Filename);
editorTabs(k).close;
end
OPTION 3:
% Close all script editor tabs, one at a time with Yes/No/Cancel prompt, and listing names of the closed files in the command window.
editorTabs = matlab.desktop.editor.getAll;
% Filenames seem to be ordered in the editorTabs list by when you opened them:
% longest ago = #1, and most recently opened is the last item on the editorTab list.
% If you want to see their names first, without closing them, run this loop:
% for k = length(editorTabs) : -1 : 1 % Use this to see them in reverse order: most recent to oldest.
for k = 1 : length(editorTabs) % Use this to see them in original order: oldest to most recent.
fprintf('(#%2d of %2d) Filname of editorTab(%d) =\t%s. \n', k, length(editorTabs), k, editorTabs(k).Filename);
end
% Go through the list asking them which files they want to close the editor tab for.
for k = 1 : length(editorTabs)
% Prompt user if they want to close this one file.
promptMessage = sprintf('Do you want to close %s?', editorTabs(k).Filename);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Cancel', 'No');
if contains(buttonText, 'Yes', 'IgnoreCase', true)
% Close this file.
editorTabs(k).close;
fprintf('Closed %s.\n', editorTabs(k).Filename);
elseif contains(buttonText, 'Cancel', 'IgnoreCase', true)
% Break out of the loop and quit asking about any more files.
break;
end
end
CLOSE DOWN VARIABLE EDITOR WINDOWS:
In addition, it may be useful to close down all Variable Editor Windows that you might have opened by double clicking the variable name in the Workspace panel, or by putting the cursor on the variable in your script and typing Control-d.
% Close down all Variable Editor windows.
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
Titles = desktop.getClientTitles;
editorTabs = desktop.getGroupMembers('Editor')
for k = 1 : numel(Titles)
Client = desktop.getClient(Titles(k));
if ~isempty(Client)
thisName = char(Client.getClass.getName);
fprintf('Closing Variable Editor window for #%d = %s\n', k, thisName);
% Close variable editor window.
if contains(thisName, 'com.mathworks.mde.array.ArrayEditor')
Client.close();
end
end
end
Xiangrui Li
on 25 Apr 2022
1 vote
I suppose you may also close the Editor window, if you wanted to close all scripts.
Why does not do the following simple way?
close(matlab.desktop.editor.getAll)
Categories
Find more on System Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!