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

Customizing Workspace context-menu

November 3, 2010 19 Comments

My first article of 2010 described customizing Matlab’s Workspace table, in particular the values presented in the Bytes column.
Last week, a reader of that article posted a comment asking how to customize the context (right-click) menu with some user-defined actions. The user has found that modifying the menu via the regular table handles gets reset automatically.
Today’s article will describe an easy and effective way to add user-defined actions to the Workspace table, for specific object types.
The trick is not to modify the context-menu directly. As the reader above has noticed, this menu is automatically recreated on the fly, so no change will be persistent. Instead, we modify Matlab’s internal registry of class-specific context-menus. This is done in several prefspanel.m files in the Matlab codebase (For example: \toolbox\matlab\audiovideo\prefspanel.m and \toolbox\signal\signal\prefspanel.m).
The mechanism relies on the following Java method, which is unsupported and undocumented, yet has existed in the present form for the past several releases:

classes = {'double', 'java.lang.Object'};
menuName = 'My context-menu';
menuItems = {'Inspect', 'Properties', '-', 'class name'};
menuActions = {'inspect($1)', 'get($1)', '', 'class($1)'};
com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry.registerClassCallbacks(classes,menuName,menuItems,menuActions);

classes = {'double', 'java.lang.Object'}; menuName = 'My context-menu'; menuItems = {'Inspect', 'Properties', '-', 'class name'}; menuActions = {'inspect($1)', 'get($1)', '', 'class($1)'}; com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry.registerClassCallbacks(classes,menuName,menuItems,menuActions);

Customizable Workspace table context-menu
Customizable Workspace table context-menu

Once you have found your particular context-menu configuration useful, place the short customization code in your startup.m file so that the change becomes permanent in all future Matlab sessions.
A few other supporting static methods are available in the com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry class: getClassCallbacksInformation (className), registerSimilarClassCallbacks(newClassNames,definedClassName) and unregisterClassCallbacks(definedClassName). For example:

com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry.getClassCallbacksInformation('double')
ans =
java.lang.Object[]:
    'My context-menu'
    [4 element array]        % = menuItems
    [4 element array]        % = menuActions

com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry.getClassCallbacksInformation('double') ans = java.lang.Object[]: 'My context-menu' [4 element array] % = menuItems [4 element array] % = menuActions

Related posts:

  1. Customizing Matlab's Workspace table – The Matlab Desktop's Workspace pane table can be customized, as described here...
  2. PlotEdit context-menu customization – A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...
  3. Adding a context-menu to a uitree – uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....
  4. Customizing uiundo – This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...
  5. Customizing help popup contents – The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....
  6. Context-Sensitive Help – Matlab has a hidden/unsupported built-in mechanism for easy implementation of context-sensitive help...
Desktop Internal component Java Undocumented feature
Print Print
« Previous
Next »
19 Responses
  1. Donn Shull November 4, 2010 at 10:07 Reply

    It may be worth noting for people developing toolboxes that at startup MATLAB searches the path for prefspanel.m and executes them. An alternative location for context-menu configurations is in your own prefspanel.m file.

    Thanks Yair

  2. Mike L November 7, 2010 at 13:13 Reply

    Works like a charm! I’m amazed so much of the underlying machinery is exposed and how straightforwardly you were able to go ahead and play with it. Thanks, Yair!

    • Yair Altman November 7, 2010 at 14:46 Reply

      Thanks Mike – it may look straightforward when I present it (which is exactly my purpose), but believe me when I say that each of these seemingly simple posts is the result of many hours of research, trial-and-error, tests, blind-alleys etc….

  3. Bluesmaster March 7, 2013 at 10:02 Reply

    is this also possible for the “current folder” component?
    I would like to add an entry for tortoise svn commit for single files

    best regards

    Bluesmaster

    • Yair Altman March 7, 2013 at 10:10 Reply

      @Bluesmaster – I don’t think so. There’s probably a similar mechanism somewhere else, but I don’t think it’s under com.mathworks.mlwidgets.workspace.*. Then again, I never checked so maybe it is… Let us all know if you discover anything interesting.

      • Bluesmaster March 7, 2013 at 10:18

        hm too bad. And what about a workaround? Maybe a button in the “current folder” toolbar?
        They also have to know which file is selected e.g. for creating a new folder into the selected one.

        Do you have an idea?

        Thanks

      • Yair Altman March 7, 2013 at 11:04

        @Bluemaster – I never tried this but I’m sure this is also possible. I showed how to customize the Desktop toolbar in section 8.1.4 of my Matlab-Java book, and you could start from there.

  4. Bluesmaster March 8, 2013 at 10:56 Reply

    ok got it going, thanks to your advice

    http://www.mathworks.com/matlabcentral/fileexchange/40685-tortoise-svn-toolbar-integration

    The deciding lines are:

    com.mathworks.mde.explorer.Explorer.getInstance.getComponents
    com.mathworks.mde.explorer.Explorer.getInstance.getTable

    com.mathworks.mde.explorer.Explorer.getInstance.getComponents com.mathworks.mde.explorer.Explorer.getInstance.getTable

    Got some questions/ advices:

    – what about a diff-tool in findjobj (like regshot), return of investment could be reached in days

    – what about a small search line in findjobj ( popup( ‘class’ , ‘property’ …) | searchstring)

    – copying classnames to clipboard could also be usefull (lost an hour by a typing error 🙁 )

    – how to get informations about com.mathworks.* without having an entity? (just had luck, that I “discovered” .getInstance)

    sorry for the big amount of text, and best regards

    Bluesmaster

    • Yair Altman March 9, 2013 at 16:55 Reply

      I think you may be trying to turn findjobj into a lot more than it was intended for…

      For a diff tool, try my objdiff utility.

      For the object classname, simply copy the object to the workspace (using one of the right-click context menu options) and then run the class function on it (class(h))

      For basic information about a class you could use either my checkClass utility or my uiinspect utility. For documentation on the internal classes, read my Matlab-Java book.

      • Bluesmaster March 10, 2013 at 01:53

        “I think you may be trying to turn findjobj into a lot more than it was intended for…”

        …that is possible, but I thought gathering ideas is never a bad idea.

        objdiff is great, but it doesn’t support recursion ( ? ) so it does not suite for the purpose of “Checking a checkbox and find out the corresponding java-obj that changed”.
        That’s what I meant about “Return of investment”, cause I thought your work is a lot about investigation ( of course today you seem to know the most things, but some newcomers like me maybe not 🙂 )

        Thanks for your help

        Bluesmaster

  5. Bluesmaster March 12, 2013 at 07:02 Reply

    Hello Yair,

    I need a small advice. I got access to the currentfileBrowser. It is a JideSoft TreeTable (i think)

    cfb = com.mathworks.mde.explorer.Explorer.getInstance.getTable

    I searched for hours, but i cant find a way to customize a cells style (icon/ background…)
    You have a lot of experience with the jide-Tools can you tell me how to to do that if I got lets say this item:

    cfb.getItem(0)

    That would help me so much.

    best regards

    Bluesmaster

    • Yair Altman March 12, 2013 at 14:29 Reply

      @Bluesmaster – Answering your query is non-trivial and goes beyond what I can provide in a simple comment here.

      You should read either my book or my uitable report as a starting point. I can also help you customize your GUI as a consultant if you wish.

  6. CrisPi June 14, 2013 at 06:30 Reply

    Hi,
    great and super useful article, but I ran into a bit of a problem – the classes ‘single’ and ‘double’ only work when they are not complex (Matlab 2013a). I haven’t had any luck finding the appropriate class name for the complex case (e.g. ‘complex single’) – could you please help me out?
    Thanks, Chris

    • Malcolm Lidierth June 14, 2013 at 18:05 Reply

      @CrisPi

      Complex numbers do not have a special type: a complex number is stored as two numbers representing its real and imaginary types, so e.g

      >> i
      ans =
              0 + 1.0000i
       
      >> isreal(imag(i))
      ans = 
              true

      >> i ans = 0 + 1.0000i >> isreal(imag(i)) ans = true

    • Yair Altman June 15, 2013 at 12:19 Reply

      @CrisPi – all you need to do is to add the class which is specified in the Workspace’s Class column (in this case, 'double (complex)' or 'single (complex)'), and then restart Matlab. As easy as that…

      …and similarly for any user defined or third-party class…

      classes = {'double', 'single', 'double (complex)', 'single (complex)', 'java.lang.Object', 'com.mathworks.mde.desk.MLDesktop'};
      ...

      classes = {'double', 'single', 'double (complex)', 'single (complex)', 'java.lang.Object', 'com.mathworks.mde.desk.MLDesktop'}; ...

    • CrisPi August 20, 2013 at 07:55 Reply

      Thanks so much Yair, that worked – I was looking at the Value column, not the Class one.

      But on a related note: Is it also possible to define a command for multiple choices? ie selecting two, and then the command is executed on both of them (or they are combined, like it’s done for the plot(var1,var2) possibility)…

  7. Lex July 26, 2017 at 10:16 Reply

    Thank you, Yair! I use Matlab 2017a and make menu for Java class ‘java.lang.Object’. But this menu item appears only for this class and don’t take inheritance into account. Can I make menu item for all Java variables?

  8. MatthieuC April 23, 2018 at 15:06 Reply

    Hello !
    Is there an equivalent for the “Current Folder” context-menu?
    I know the package is com.mathworks.mlwidgets.explorer but I cannot handle the way of adding context-menu with it.

    Thanks

    • MatthieuC April 24, 2018 at 14:26 Reply

      To give more detail about my question… I would add some Subversion specific actions on Right-Click (for instance, setting working copy depth commands).
      Thank you for your help.

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