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

364 次查看(过去 30 天)

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2020-4-2
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:

更多回答(2 个)

Image Analyst
Image Analyst 2020-4-2
编辑:Image Analyst 2020-4-2
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 2022-4-25
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)

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by