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');
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'
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:
- setPrompt – Setting the Matlab Desktop prompt The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...
- Setting desktop tab completions The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...
- Matlab layout managers: uicontainer and relatives Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....
- 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....
- Accessing the Matlab Editor The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....
- 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....
Categories: Desktop, High risk of breaking in future versions, Java
This entry was posted
on Wednesday, June 24th, 2009 at 1:47 pm 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.
