Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Setting the Matlab desktop layout programmatically

June 24, 2009 6 Comments

The Matlab desktop enable users to switch between different presentation layouts of the desktop panels (Command Window, Workspace etc.). This has been supported as far back as Matlab 6 (R12), with newer Matlab releases adding improved functionality such as the ability to save user-defined layouts, as Kristin explained in the official Matlab desktop blog.
The only supported way to save and switch layouts is to use the desktop’s main menu. Since Kristin has posted her write-up, a few people have posted unanswered follow-up comments requesting to know how to programmatically save and switch layouts. I will now show how this can be done.
First, we need to get the Java handle of the Matlab desktop. We can then investigate this handle using the built-in methodsview function or my UIInspect utility on the File Exchange. We quickly see the relevant layout-related functions, which we can put to good use:

% Get the desktop's Java handle (Matlab 7 only)
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
% Inspect the available desktop functions
methodsview(desktop);
uiinspect(desktop);
% Save the current layout
desktop.saveLayout('Yair');
% Switch between different layouts
desktop.restoreLayout('Yair');
desktop.restoreLayout('Default');
desktop.restoreLayout('History and Command Window');

% Get the desktop's Java handle (Matlab 7 only) desktop = com.mathworks.mde.desk.MLDesktop.getInstance; % Inspect the available desktop functions methodsview(desktop); uiinspect(desktop); % Save the current layout desktop.saveLayout('Yair'); % Switch between different layouts desktop.restoreLayout('Yair'); desktop.restoreLayout('Default'); desktop.restoreLayout('History and Command Window');

Desktop layout menu in Matlab 7
Desktop layout menu in Matlab 7

Note that trying to restore an invalid layout name simply does nothing (does not throw an error).
Also note that this relies heavily on unsupported and undocumented internal implementation, which may change without prior notice between Matlab releases. The code snippet above works on several Matlab 7 releases. But for Matlab 6.0 (R12), for example, it needs to be modified:

% Get the desktop's Java handle (Matlab 6 only)
desktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop;
% Inspect the available desktop functions
% Note: in Matlab 6, methodsview() did not accept object handles
methodsview('com.mathworks.ide.desktop.MLDesktop');
%uiinspect(desktop);  % UIINSPECT does not work on Matlab 6
% Save the current layout
% saving the desktop is not possible in Matlab 6
% Switch between different layouts
desktop.set5PanelLayout;
desktop.setTallLayout;
desktop.setShortLayout;
desktop.setDefaultDesktop;
desktop.setMolerMode;        % ='Command window only'

% Get the desktop's Java handle (Matlab 6 only) desktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop; % Inspect the available desktop functions % Note: in Matlab 6, methodsview() did not accept object handles methodsview('com.mathworks.ide.desktop.MLDesktop'); %uiinspect(desktop); % UIINSPECT does not work on Matlab 6 % Save the current layout % saving the desktop is not possible in Matlab 6 % Switch between different layouts desktop.set5PanelLayout; desktop.setTallLayout; desktop.setShortLayout; desktop.setDefaultDesktop; desktop.setMolerMode; % ='Command window only'

Desktop layout menu in Matlab 6
Desktop layout menu in Matlab 6

Note that Matlab 6 did not have a generic restoreLayout() function, instead using a few pre-defined setXXX(). Also note that Matlab 6 did not have the saveLayout() function (it did have saveDesktop(string,string) and restoreDesktop(string,string), which I leave as an excercise to readers).
This has been the first example of many useful things that can be done with the Matlab desktop handle. In the future I will describe other aspects. Perhaps the main lesson to be learned from this post is that essentially anything that can be done via the menu can also be done programmatically.

Related posts:

  1. setPrompt – Setting the Matlab Desktop prompt – The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...
  2. Setting desktop tab completions – The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...
  3. Changing system preferences programmatically – Matlab user/system preferences can be changed programmatically, from within your Matlab application or from the Matlab desktop command prompt. This post details how....
  4. Setting status-bar text – The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....
  5. Matlab layout managers: uicontainer and relatives – Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....
  6. Listbox layout customization – Matlab's listbox layout can be modified to display multiple item columns and different inter-cell margins. ...
Desktop Java
Print Print
« Previous
Next »
6 Responses
  1. Troykapoika March 16, 2011 at 01:06 Reply

    I would like to know if there is any way to organize (remove) layouts programmatically? I have played around with uiinspect looking for the calls to organize and remove layouts, but I haven’t been too successful. Also, do you know if there is anyway to share layouts between computers and load them into MATLAB without having to restart MATLAB?

    Thanks!

    Troy

  2. Sky October 24, 2013 at 02:23 Reply

    This post inspired me to make a small utility (now on the File Exchange) that I personally use very often and find quite useful. Hopefully others will too. Thank you for the post. I’d love to get feedback on the file if you have any for me.

    • Yair Altman October 24, 2013 at 02:35 Reply

      nice 🙂

  3. Anderson April 29, 2014 at 12:40 Reply

    Hi,

    Is there a command similar to desktop.closeCommandWindow, so that I can close/open editor window and figures window?

    Thank you

    • Yair Altman April 29, 2014 at 12:50 Reply

      @Anderson:

      close(matlab.desktop.editor.findOpenDocument('myMatlabFunction.m'))  % close editor file
       
      close(findall(0, 'type','figure', 'name','My figure'))  % close a specific figure window
       
      jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
      jDesktop.closeGroup('Figures');  % close the Figures container

      close(matlab.desktop.editor.findOpenDocument('myMatlabFunction.m')) % close editor file close(findall(0, 'type','figure', 'name','My figure')) % close a specific figure window jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance; jDesktop.closeGroup('Figures'); % close the Figures container

  4. Dominik March 24, 2016 at 11:47 Reply

    Dear Yair,

    thank you for all those helpful matlab secrets!
    I was wondering how to arrange figures in my tiled custom desktop group.
    The group I create like that:

    desktop.setDocumentArrangement(groupname, 2, java.awt.Dimension(nXPanes, nYPanes));

    desktop.setDocumentArrangement(groupname, 2, java.awt.Dimension(nXPanes, nYPanes));

    And the figures I associate via:

     set(get(handle(o.figure), 'javaframe'), 'GroupName', groupname);

    set(get(handle(o.figure), 'javaframe'), 'GroupName', groupname);

    Now I would ideally like to control in which pane the figures are created and, possibly be able to create tabbed figures within one pane.
    I would be delighted about any hint.

    Thank you!

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top