How to create Favorites by code / Command Window?

41 visualizaciones (últimos 30 días)
sfreeman
sfreeman el 24 de Jul. de 2018
Comentada: Thomas el 7 de Oct. de 2025 a las 8:23
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?

Respuesta aceptada

Martin Lechner
Martin Lechner el 23 de Nov. de 2018
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)
  13 comentarios
Nandan
Nandan el 14 de Mayo de 2025
How do I add a Simulink favorite to the quick access toolbar programmatically? com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance() only give me the MATLAB favorites but not Simulink ones. Thank you.
Thomas
Thomas el 7 de Oct. de 2025 a las 8:23
The solution was working fine for several releases, but with R2025b the last command:
fc.addCommand(newFavoriteCommand)
throws an error:
>> fc.addCommand(newFavoriteCommand)
ans =
[]
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.addCategoryIfNecessary(FavoriteCommands.java:575)
at com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.addCommand(FavoriteCommands.java:259)
at com.mathworks.mlwidgets.favoritecommands.FavoriteCommands$3.run(FavoriteCommands.java:249)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Any idea how to do this in R2025b?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Java from MATLAB en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by