- Undocumented Matlab - http://undocumentedmatlab.com -

Customizing Workspace context-menu

Posted By Yair Altman On November 3, 2010 | 19 Comments

My first article of 2010 described customizing Matlab’s Workspace table [1], in particular the values presented in the Bytes column.
Last week, a reader of that article posted a comment [2] 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);

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 [3] 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

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


19 Comments (Open | Close)

19 Comments To "Customizing Workspace context-menu"

#1 Comment By Donn Shull On November 4, 2010 @ 10:07

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 Comment By Mike L On November 7, 2010 @ 13:13

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!

#3 Comment By Yair Altman On November 7, 2010 @ 14:46

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….

#4 Comment By Bluesmaster On March 7, 2013 @ 10:02

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

#5 Comment By Yair Altman On March 7, 2013 @ 10:10

@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.

#6 Comment By Bluesmaster On March 7, 2013 @ 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

#7 Comment By Yair Altman On March 7, 2013 @ 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.

#8 Comment By Bluesmaster On March 8, 2013 @ 10:56

ok got it going, thanks to your advice

[10]

The deciding lines are:

 
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

#9 Comment By Yair Altman On March 9, 2013 @ 16:55

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

For a diff tool, try my [11] 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 [12] utility or my [13] utility. For documentation on the internal classes, read my Matlab-Java book.

#10 Comment By Bluesmaster On March 10, 2013 @ 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

#11 Comment By Bluesmaster On March 12, 2013 @ 07:02

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

#12 Comment By Yair Altman On March 12, 2013 @ 14:29

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

You should read either my [14] or my [15] as a starting point. I can also help you customize your GUI as a consultant if you wish.

#13 Comment By CrisPi On June 14, 2013 @ 06:30

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

#14 Comment By Malcolm Lidierth On June 14, 2013 @ 18:05

@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

#15 Comment By Yair Altman On June 15, 2013 @ 12:19

@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'};
...

#16 Comment By CrisPi On August 20, 2013 @ 07:55

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)…

#17 Comment By Lex On July 26, 2017 @ 10:16

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?

#18 Comment By MatthieuC On April 23, 2018 @ 15:06

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

#19 Comment By MatthieuC On April 24, 2018 @ 14:26

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.


Article printed from Undocumented Matlab: http://undocumentedmatlab.com

URL to article: http://undocumentedmatlab.com/articles/customizing-workspace-context-menu

URLs in this post:

[1] customizing Matlab’s Workspace table: http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/

[2] posted a comment: http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/#comment-21170

[3] startup.m file: http://blogs.mathworks.com/loren/2009/03/03/whats-in-your-startupm/

[4] Customizing Matlab's Workspace table : http://undocumentedmatlab.com/articles/customizing-matlabs-workspace-table

[5] PlotEdit context-menu customization : http://undocumentedmatlab.com/articles/plotedit-context-menu-customization

[6] Adding a context-menu to a uitree : http://undocumentedmatlab.com/articles/adding-context-menu-to-uitree

[7] Customizing uiundo : http://undocumentedmatlab.com/articles/customizing-uiundo

[8] Customizing help popup contents : http://undocumentedmatlab.com/articles/customizing-help-popup-contents

[9] Context-Sensitive Help : http://undocumentedmatlab.com/articles/context-sensitive-help

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

[11] : http://www.mathworks.com/matlabcentral/fileexchange/14395-objdiff-generic-object-comparator

[12] : http://www.mathworks.com/matlabcentral/fileexchange/26947-checkclass

[13] : http://undocumentedmatlab.com/blog/uiinspect/

[14] : http://undocumentedmatlab.com/matlab-java-book/

[15] : http://undocumentedmatlab.com/blog/uitable-customization-report/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.