Accessing the Matlab Editor

Monday, October 19th, 2009

Matlab’s built-in editor, like most other Matlab GUI, is Java-based. As such, it can easily be accessed programmatically. ImageAnalyst, a well-respected member of the Matlab community and a frequent CSSM (newsgroup) and FEX (File Exchange) contributor, recently asked whether it is possible to retrieve the name of the Editor’s currently edited file. The answer is that this is very easy, but I decided to use this opportunity to show how other interesting things can be done with the Editor.

Before we start, it should be made clear that this entire article relies on MathWorks internal implementation of the Editor and Desktop, which may change without prior notice in future Matlab releases. The code below appears to work under Matlab 6 & 7, but users who rely on forward compatibility should be aware of this warning.

We start by retrieving the Editor handle. This can be done in a number of ways. The easiest is via the Matlab desktop:

try
    % Matlab 7
    desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
    jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
    % we get a com.mathworks.mde.desk.MLMultipleClientFrame object
catch
    % Matlab 6
    % Unfortunately, we can't get the Editor handle from the Desktop handle in Matlab 6:
    %desktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop;
 
    % So here's the workaround for Matlab 6:
    openDocs = com.mathworks.ide.editor.EditorApplication.getOpenDocuments;  % a java.util.Vector
    firstDoc = openDocs.elementAt(0);  % a com.mathworks.ide.editor.EditorViewContainer object
    jEditor = firstDoc.getParent.getParent.getParent;
    % we get a com.mathworks.mwt.MWTabPanel or com.mathworks.ide.desktop.DTContainer object
end

Now that we have the Editor handle, let’s retrieve its currently open (active) file name from the Editor’s title:

title = jEditor.getTitle;
currentFilename = char(title.replaceFirst('Editor - ',''));

The entire list of open file names can be retrieved in several ways:

% Alternative #1:
edhandle = com.mathworks.mlservices.MLEditorServices;
allEditorFilenames = char(edhandle.builtinGetOpenDocumentNames);
 
% Alternative #2:
openFiles = desktop.getWindowRegistry.getClosers.toArray.cell;
allEditorFilenames = cellfun(@(c)c.getTitle.char,openFiles,'un',0);

At the top-level Editor-window level, we can prevent its resizing, update its status bar, modify its toolbar/menu-bar, control docking and do other similar fun things:

% Actions via built-in methods:
jEditor.setResizable(0);
jEditor.setStatusText('testing 123...');
jEditor.setTitle('This is the Matlab Editor');
 
% Equivalent actions via properties:
set(jEditor, 'Resizable', 'off');
set(jEditor, 'StatusText', 'testing 123...');
set(jEditor, 'Title', 'This is the Matlab Editor');

Actually, the jEditor handle has over 300 invokable methods and close to 200 properties that we can get/set. Perhaps the easiest way to find interesting things we can programmatically do with the Editor handle, is to use my UIInspect utility on the File Exchange:

uiinspect(jEditor);  % or: jEditor.uiinspect
Matlab Editor methods, callbacks and properties as seen by uiinspect (click to zoom)

Matlab Editor methods, callbacks and properties as seen by uiinspect
(click to zoom)

The Editor handle is actually a container for many internal panels (toolbars etc.) and documents. The entire object hierarchy can be seen with another of my File Exchange utilities, FindJObj:

findjobj(jEditor);  % or: jEditor.findjobj
Matlab Editor object hierarchy as seen by findjboj (click to zoom)

Matlab Editor object hierarchy as seen by findjboj (click to zoom)

We can modify text within the open Editor documents, and instrument these document to handle event callbacks. To see how, I refer users to my EditorMacro utility on the Matlab File Exchange.

If you find some other nifty and/or useful things that can be done using the Editor handle, please post them in the comments section below.

Related posts:

  1. EditorMacro – assign a keyboard macro in the Matlab editor EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....
  2. Non-textual editor actions The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...
  3. Setting the Matlab desktop layout programmatically The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....
  4. setPrompt – Setting the Matlab Desktop prompt The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...
  5. Rich Matlab editbox contents The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....
  6. FindJObj – find a Matlab component’s underlying Java object The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....

Tags: , , , ,

Categories: Desktop, High risk of breaking in future versions, Java

This entry was posted on Monday, October 19th, 2009 at 11:00 am PST
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

PoorSo-soHelpfulVery helpfulExcellent! (No Ratings Yet)
Loading ... Loading ...
Bookmark and Share Print Print

12 Responses to “Accessing the Matlab Editor”

  1. To close all M-files in the editor, I use:

     
    com.mathworks.mlservices.MLEditorServices.closeAll
  2. Ruben Faibish Ruben Faibish says:

    Do you know how can I find the bookmarks in the editor? I would like to get all bookmarks and save them on closing Matlab, so that on reopening, they could be restored.
    Thx

  3. Yair Altman Yair Altman says:

    Ruben – that was a hard question that kept me digging for hours. I discovered (via the FindJObj utility) that the bookmarks are displayed in a panel called ‘ExecutionArrowDisplay’ of class com.mathworks.mde.editor.ExecutionArrowDisplay$ExecutionPanel. I do not know whether the list of bookmarks are contained within this object, although I suspect they are. In any case, I did not find a method to programmatically retrieve this list since the ExecutionPanel object does not expose any method to do so.

    So, the only way I could find is to loop over all the editor’s open files, and invoke the editor’s default action for <F2> (which is the ‘next-bookmark’ action reported by the EditorMacro utility) in a loop until you detect coming back to an earlier line number. It’s ugly, but it should work. The reverse, setting bookmarks, could be done in a similar fashion, using the ‘toggle-bookmark’ action.

    I think it could be a great File Exchange utility. Care to try it? If you get stuck I can help out.

  4. Mike Katz Mike Katz says:

    @Aurélien, in MATLAB >= R2009b you can try

    editorservices.closeGroup

    And to leave the group up and just close all the open editors

    e=editorservices.getAll; e.close

    For more about the “less undocumented” way to access the editor, see my new post:
    http://blogs.mathworks.com/desktop/2009/10/26/the-matlab-editor-at-your-fingertips/

  5. Matthew Arthington Matthew Arthington says:

    My editor window is docked and that stops title being the ‘Editor – ‘… form. I wanted to get the active document so I could invoke a command to open the directory of the current editor’s file.

    Thanks to Aurélien I discovered the following to get the current editor’s file path:

    d = com.mathworks.mlservices.MLEditorServices;
    fName = d.builtinGetActiveDocument;
    path=fileparts(fName.toCharArray');
  6. Yair Altman Yair Altman says:

    For anyone interested, Jan Simon has just posted a few more useful things that can be done using the Editor handle:

    Bring the editor to front:

    jEditor.toFront;

    Is the editor active currently:

    jEditor.isActive;

    Perform an action whenever the user activate the editor:

    set(jEditor, 'WindowGainFocusCallback', 'disp(''Editor activated'')')

    Or the usual function handle callback style.

    See further properties:

    get(jEditor)

    and

    methods(jEditor, '-full')
  7. Jan Simon Jan Simon says:

    This does not work on my Matlab 6.5.1, WinXP SP3, JavaVM 1.3.1_01

    % So here's the workaround for Matlab 6:
    openDocs = com.mathworks.ide.editor.EditorApplication.getOpenDocuments;

    There is no getOpenDocuments method for the EditorApplication.
    The solution is trivial: Do not use Matlab 6.5… Thanks, Jan

  8. Yair Altman Yair Altman says:

    @Jan – I’m willing to bet that the solution for 6.5 is simply a variant of the Matlab 6 syntax. Try to run the following command in Matlab 6.5 to check whether the getOpenDocuments() method was renamed:

    methodsview('com.mathworks.ide.editor.EditorApplication')
  9. Jan Simon Jan Simon says:

    Nope. The EditorApplication knows: closeAll, closeDocument, equals, getClass, hashCode, isDocumentDirty, isStandalone, main, newDocument, newDocumentWithString, notify, notifyAll, openDocument, openDocumentToFunction, openDocumentToLine, reloadDocument, saveDocument, showJitZones (what’s that?!), toString and wait.
    Surprising. Jan

  10. christian rock christian rock says:

    Hi Yair!,

    thank you for the undocumented stuff! really great!
    Now i have one question: would it be possible to add a uitree to the editor? It would be great to browse through the opened files which are grouped by “projects” or something, especially if you opened large number of files…better than then a simple list.

    Greeting form Germany,

    Chris

  11. Yair Altman Yair Altman says:

    @Christian – you can’t [easily] add a tree to the Editor, but you can create a simple tree in a regular Matlab figure (use the built-in uitree function), set the node callbacks to something like: edit(filename) and then dock this figure into the Editor (yes I know this sounds a bit strange) using my setFigDockGroup utility.

  12. Omid Omid says:

    Hi,
    I sent this question on MATLAB central but received no answer, so I post it here again in case you’ve not seen it!

    I have a time tracking software that records the time I spend on each application. It recognizes the applications by their window caption. I’m used to have the MATLAB editor docked into the main window, so all my working time in MATLAB is reported under the name of “MATLAB 7.8.0 (2009a)”. I’m thinking of a way to change the main window title to the name of the document I’m currently working on. Thanks to undocumentedmatlab.com now I know how to get the name of the active document and how to change the window title by accessing related java components. What I need is to code a callback function to be triggered whenever I switch to another document in the editor. However I didn’t find the right callback property. Any idea?

    P.S. What I said I know works when the editor is undocked which is not what I’m looking for! So I correct that: I don’t even know how to get the title of the active document in the editor when the editor is docked into the main window :(

Leave a Reply


Wrap code fragments inside <pre lang="matlab"> tags, like this:

   <pre lang="matlab">
   a = magic(3);
   sum(a)
   </pre>