I found a solution on the web (see below, circa 2009) which does not work on my machine (Windows 7, Matlab R2013a):
Editor = com.mathworks.mlservices.MLEdito
As you noticed, newer versions of Matlab do not return the same type of Java
object for the editor.
The main editor service can still be accessed with the same command as before:
edtSvc = com.mathworks.mlservices.MLEditorServices ; %// get the main editor service ;
But this only return the handle of the service, not individual editor.
As answered by Daniel, you can close the full service from there, which will close all the editors at once. You can use one of the 2 methods available:
edtSvc.getEditorApplication.close ; %// Close all editor windows. Prompt to save if necessary.
edtSvc.getEditorApplication.closeNoPrompt ; %// Close all editor windows, WITHOUT SAVE!!
Now in this version, each file opened is actually an instance of an editor object. If you want control of individual editor tab/window, you can retrieve the list of the editor objects, then apply methods on them individually:
edtList = edtSvc.getEditorApplication.getOpenEditors.toArray ; %// get a list of all the opened editor
edtList =
java.lang.Object[]:
[com.mathworks.mde.editor.MatlabEditor]
[com.mathworks.mde.editor.MatlabEditor]
[com.mathworks.mde.editor.MatlabEditor]
This return a vector of com.mathworks.mde.editor.MatlabEditor
object. (I have 3 opened file in my editor for this example).
From now on, each of these object controls an individual file. You could close a single file already but you need to know which index is the file you want to target. To know which one points to what, you can query the getLongName
property:
>> edtList(1).getLongName
ans =
C:\TEMP\StackExchange\Editor_control.m
But if you will have to control individual files, I find it easier to build a structure with field names corresponding to the file names. This could be done this way:
for k=1:length(edtList) ;
[~, fname ]= fileparts( char( edtList(k).getLongName.toString ) ) ;
edt.( fname ) = edtList(k) ;
end
Now I have a structure with meaningful names (well, at least for me, your files and field names will be different of course):
>> edt
edt =
Bending_Movie_Time_Lapse: [1x1 com.mathworks.mde.editor.MatlabEditor]
Editor_control: [1x1 com.mathworks.mde.editor.MatlabEditor]
foldfunction_test: [1x1 com.mathworks.mde.editor.MatlabEditor]
So back to closing an individual file. This can be done easily with one of the same method than before:
edt.foldfunction_test.close %// close with prompt if necessary
edt.foldfunction_test.closeNoPrompt %// close immediately without save
Note that at this stage, you also have access to a nice list of methods and properties for your editor file. You can have a look at them by using the autocompletion (Tab key) of Matlab.
Example done on Matlab R2013a / Windows 7 64 bits
The following seems to work. I've tested in Matlab R2014b, Windows 7 64 bits.
D
.D
times to close all open files. Optionally, send also N keystrokes D
times in case some file is not saved and you want to close it (i.e. reply "no" when the editor asks if you want to save it). If the file is already saved, sending an N causes no harm.For step 1 I found inspiration in this post. For steps 2 and 3 I inspected the methods of the editor object until I found something interesting. For step 4 I took the procedure I used in this answer, which in turn was based on this information.
Code:
closeUnsaved = 1; %// 1 if you want to close even if documentds are not saved
%// Step 1:
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor; %// editor object
%// Step 2:
D = jEditor.getGroup.getDocumentCount;
%// Step 3:
jEditor.requestFocus; %// make editor the window in front
%// Step 4:
robot = java.awt.Robot;
for n = 1:D
robot.keyPress (java.awt.event.KeyEvent.VK_ALT); %// press "ALT"
robot.keyPress (java.awt.event.KeyEvent.VK_F4); %// press "F4"
robot.keyRelease (java.awt.event.KeyEvent.VK_F4); %// release "F4"
robot.keyRelease (java.awt.event.KeyEvent.VK_ALT); %// release "ALT"
if closeUnsaved
robot.keyPress (java.awt.event.KeyEvent.VK_N); %// press "N"
robot.keyRelease (java.awt.event.KeyEvent.VK_N); %// release "N"
end
end