MATLAB Answers

sfreeman
4

How to create Favorites by code / Command Window?

Asked by sfreeman
on 24 Jul 2018
Latest activity Commented on by Michel Bertrand on 19 Mar 2019
I was used to create the shortcuts in R2017b and older via code:
jUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
jUtils.addShortcutToBottom(sName,sCcallback,sIcon,sCategory, 'true');
Of course it does not work to create the new "favorites", but I would like to do so.
I have found the com.mathworks.mlwidgets.favoritecommands.FavoriteCommandProperties class with get/set methods, but if I got it right, I would Need some stuff from com.mathworks.mlwidgets.favoritecommands.FavoriteCommandActions, which has not constructor.
Any ideas out there, how to create favorites and their categories by code?

  0 Comments

Sign in to comment.

Products


Release

R2018a

1 Answer

Answer by Martin Lechner on 23 Nov 2018
 Accepted Answer

For managing the favorites use the FavoriteCommands class. The available methods can be seen by using methodsview:
fc = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance()
methodsview(fc) % to show all available methods with their parameters
Before you add a new favorite command with the FavoriteCommands class you must create your FavoriteCommandProperties object, for example like:
newFavoriteCommand = com.mathworks.mlwidgets.favoritecommands.FavoriteCommandProperties()
newFavoriteCommand.setLabel("Programatically added Favorite")
newFavoriteCommand.setCategoryLabel("MY CREATED CATEGORY") % use always upper case letters, otherwise I got problems to add furterh favorits
newFavoriteCommand.setCode("disp('Hallo World! This greate command was added programatically, by com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance()')")
newFavoriteCommand.setIsOnQuickToolBar(true)
% add the command to the favorite commands (the category is automatically created if it doesn't exist)
fc.addCommand(newFavoriteCommand)

  5 Comments

Thanks Julien for your additional informations according the the difference between label and name of categories or favorite commands.
The FavoriteCommands class provides a protected method called getCategories where you can get the categories. The code below shows how to call this method by using java reflections. To remove the categorie you need the name and not the label of the category (shown in the last command).
%% Getting all available categories
fc = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance()
%method = fc.getClass().getMethod("getCategories", []) % only works for public methods
method = fc.getClass().getDeclaredMethod("getCategories", []) % works also for private or protected methods
method.setAccessible(true)
categories = method.invoke(fc,[]) % returns a Java List with all categories
cat = categories.get(0) % returns the first category
cat.getLabel()
cat.getName()
cat.getDescription()
icon = cat.getIcon()
icon.getIcon()
% The following command returns the childrens (favorite commands of the group)
childs = cat.getChildren()
child = childs.get(0)
child.getLabel() % the name of the command (shown in the GUI)
child.getName() % looks like an UIID number
child.getCode() % the program code of this favorite command
% To remove a complete category group calling removeCategory with the name of the category worked for me.
fc.removeCategory(cat.getName())
To create and remove categories the following code snippets gives some advice:
%% Create Categories
fc = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance()
newCategory = com.mathworks.mlwidgets.favoritecommands.FavoriteCategoryProperties()
newCategory.setLabel("MyCreatedCategory")
newCategory.getName() % returns []
retCat = fc.addCategory(newCategory) % create the new category
newCategory.getName() % returns the create UIID after adding the category to the favorites GUI
retCat.getName() % has an additional prefix user_favorite_commands_toolset:
fc.removeCategory(retCat.getName()) % doesn't remove the category
retCat2 = fc.addCategory(newCategory) % create the new category with the same lable and a different name
retCat2.getName() % has an additional prefix user_favorite_commands_toolset:
newCategory.getName() % returns now the new name
fc.removeCategory(newCategory.getName()) % removes the second category
% to remove also the firstly created category we have to extract the UUID from the returned category
str = retCat.getName()
uiid = str.split(':')
fc.removeCategory(uiid(2))
% new your favorite command doesn't show the programmatically created categories
It works pretty well, thanks a lot !
Having recently moved from Matlab2017b to 2018b, I now need to re-write some parts of my « time machine for shortcuts” I have been using for nearly ten years with shortcuts. I am implementing this for the now called Favorites, using your suggestions.
I can remove all categories but the Favorite Commands category. This is consistent with the trash icon not showing on the Favorite Commands line of the Favorite drop-down menu, but showing on all the other categories that were added as new category.
Still I want to clean up the Favorites Commands category I need to remove all the favorites (children) it contains. And there is no removeCommand, which would have made this easy; and I have tried all sort of things. I may find the way to do this the more I look at it, but perhaps you have a suggestion?

Sign in to comment.