MATLAB Answers


How to create Favorites by code / Command Window?

さんによって質問されました 2018 年 7 月 24 日
最新アクティビティ Michel Bertrand さんによって コメントされました 2019 年 3 月 19 日
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 件のコメント

サインイン to comment.




1 件の回答

回答者: Martin Lechner 2018 年 11 月 23 日

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()')")
% add the command to the favorite commands (the category is automatically created if it doesn't exist)

  5 件のコメント

Martin Lechner 2018 年 11 月 27 日
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
categories = method.invoke(fc,[]) % returns a Java List with all categories
cat = categories.get(0) % returns the first category
icon = cat.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.
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.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(':')
% new your favorite command doesn't show the programmatically created categories
Julien Prodigue 2018 年 11 月 27 日
It works pretty well, thanks a lot !
Michel Bertrand 2019 年 3 月 19 日
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?

サインイン to comment.

Translated by