How to close one or all currently open Matlab (*.m) files from Matlab command prompt?

前端 未结 2 1539
情歌与酒
情歌与酒 2021-01-19 07:47

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         


        
2条回答
  •  既然无缘
    2021-01-19 08:41

    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

提交回复
热议问题