如何从Matlab命令提示符关闭一个或所有当前打开的Matlab(* .m)文件?

问题描述 投票:4回答:2

我在网上找到了一个解决方案(见下文,大约2009年),这在我的机器上不起作用(Windows 7,Matlab R2013a):

Editor = com.mathworks.mlservices.MLEditorServices;
Editor.closeAll;
matlab editor
2个回答
3
投票

正如您所注意到的,较新版本的Matlab不会为编辑器返回相同类型的Java对象。

仍然可以使用与以前相同的命令访问主编辑器服务:

edtSvc  = com.mathworks.mlservices.MLEditorServices ;  %// get the main editor service ;

但这只返回服务的句柄,而不是单个编辑器。

正如丹尼尔所回答的,你可以从那里关闭全部服务,这将立即关闭所有编辑。您可以使用以下两种方法之一:

edtSvc.getEditorApplication.close ;             %// Close all editor windows. Prompt to save if necessary.
edtSvc.getEditorApplication.closeNoPrompt ;     %// Close all editor windows, WITHOUT SAVE!!

现在在这个版本中,每个打开的文件实际上是一个编辑器对象的实例。如果要控制单个编辑器选项卡/窗口,可以检索编辑器对象的列表,然后单独应用它们的方法:

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]

这将返回com.mathworks.mde.editor.MatlabEditor对象的向量。 (本例中我的编辑器中有3个打开的文件)。

从现在开始,这些对象中的每一个都控制一个单独的文件。您可以关闭单个文件,但需要知道要定位的文件是哪个索引。要知道哪一个指向什么,您可以查询getLongName属性:

>> edtList(1).getLongName
ans =
C:\TEMP\StackExchange\Editor_control.m

但是,如果您必须控制单个文件,我发现构建一个具有与文件名对应的字段名称的结构更容易。这可以这样做:

for k=1:length(edtList) ;
    [~, fname ]= fileparts( char( edtList(k).getLongName.toString ) ) ;
    edt.( fname  ) = edtList(k) ;
end

现在我有一个有意义名称的结构(好吧,至少对我来说,你的文件和字段名称当然会有所不同):

>> 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]

所以回到关闭一个单独的文件。这可以使用与之前相同的方法之一轻松完成:

edt.foldfunction_test.close           %// close with prompt if necessary
edt.foldfunction_test.closeNoPrompt   %// close immediately without save

请注意,在此阶段,您还可以访问编辑器文件的一个很好的方法和属性列表。您可以使用Matlab的自动完成(Tab键)来查看它们。


在Matlab R2013a / Windows 7 64位上完成的示例


1
投票

以下似乎有效。我在Matlab R2014b,Windows 7 64位测试过。

  1. 访问编辑器Java对象。
  2. 获取打开文档的数量,比如D
  3. 以编程方式使编辑器成为前面的窗口。
  4. 以编程方式发送ALT-F4击键D次以关闭所有打开的文件。或者,如果某些文件未保存并且您想要关闭它,也可以发送N次击键D次数(即当编辑器询问您是否要保存它时回复“否”)。如果文件已经保存,发送N不会造成任何伤害。

对于第1步,我在this post找到灵感。对于步骤2和3,我检查了编辑器对象的方法,直到我发现了一些有趣的东西。对于第4步,我采用了我在this answer中使用的程序,而this information又基于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

码:

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.