How do I close all open scripts in the Editor window?

383 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Apr 2020
Edited: MathWorks Support Team on 2 Apr 2020
There are 2 ways to close all open scripts in the Editor window:
1. Close all scripts in the Editor window programmatically:
If this is the case, you can use the following command to get the handle of all open scripts in Editor and then close them:
>> closeNoPrompt(matlab.desktop.editor.getAll);
The command is using the MATLAB Editor API. You can learn more about the API by typing the following into your command window:
>> doc matlab.desktop.editor
2. Close all scripts open in the Editor window via mouse:
This can be done by clicking the cross button on the top right of the Editor window.
Here is a screenshot indicating the location of this button:

More Answers (2)

Image Analyst
Image Analyst on 2 Apr 2020
Edited: Image Analyst on 2 Apr 2020
The following snippets may be useful for closing down:
  1. m-file editor windows, and/or
  2. 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
Xiangrui Li on 25 Apr 2022
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 Debugging and Analysis in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!