How to create Favorites by code / Command Window?

33 views (last 30 days)
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?

Accepted Answer

Martin Lechner
Martin Lechner on 23 Nov 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)
  11 Comments
Andrew Janke
Andrew Janke on 15 May 2021
Hear, hear! I'd like to retain this functionality: at my work, we load our internal Matlab libraries and applications into interactive Matlab sessions using Favorites, and I'd like to retain the ability for our codebase to set these all up for our users. (A full set of Favorites for our app suite is like 7 or 8 Favorites, and their definitions can change over time.)
Andrew Janke
Andrew Janke on 15 May 2021
Edited: Andrew Janke on 15 May 2021
Here's a little tool for inspecting Java class definitions in a deeper manner than methodsview(), to find out which protected and private things are available for you to expose via Reflection as in the techniques above:
function inspectJavaClassDefinition(jThing)
% Dump a debugging view of the method definitions for a Java class
%
% inspectJavaClassDefinition(jThing)
%
% JThing may be:
% * a java.lang.Class object which refers to the class you want to
% inspect methods for,
% * any other Java object, in which case its class is inspected
% * a Matlab string naming a Java class
%#ok<*AGROW>
if ischar(jThing) || isstring(jThing)
klass = java.lang.Class.forName(jThing);
elseif isa(jThing, 'java.lang.Class')
klass = jThing;
elseif isjava(jThing)
klass = jThing.getClass;
else
error('Invalid input type: %s', class(jThing));
end
function modsOutStr = decodeModifiers(modCode)
modsOut = string([]);
if java.lang.reflect.Modifier.isPublic(modCode)
modsOut(end+1) = "public";
end
if java.lang.reflect.Modifier.isPrivate(modCode)
modsOut(end+1) = "private";
end
if java.lang.reflect.Modifier.isProtected(modCode)
modsOut(end+1) = "protected";
end
if java.lang.reflect.Modifier.isStatic(modCode)
modsOut(end+1) = "static";
end
if java.lang.reflect.Modifier.isFinal(modCode)
modsOut(end+1) = "final";
end
if java.lang.reflect.Modifier.isAbstract(modCode)
modsOut(end+1) = "abstract";
end
if java.lang.reflect.Modifier.isStrict(modCode)
modsOut(end+1) = "strict";
end
if java.lang.reflect.Modifier.isSynchronized(modCode)
modsOut(end+1) = "synchronized";
end
if java.lang.reflect.Modifier.isTransient(modCode)
modsOut(end+1) = "transient";
end
if java.lang.reflect.Modifier.isVolatile(modCode)
modsOut(end+1) = "volatile";
end
if java.lang.reflect.Modifier.isNative(modCode)
modsOut(end+1) = "native";
end
if java.lang.reflect.Modifier.isInterface(modCode)
modsOut(end+1) = "interface";
end
if isempty(modsOut)
modsOutStr = "";
else
modsOutStr = strjoin(modsOut, " ");
end
end
function out = categoricalizeTable(tbl)
vars = tbl.Properties.VariableNames;
for i = 1:width(tbl)
if isstring(tbl.(vars{i})) && ~isequal(vars{i}, 'Modifiers')
tbl.(vars{i}) = categorical(tbl.(vars{i}));
end
end
out = tbl;
end
function out = parms2str(parms)
parmsStrs = string([]);
for iParm = 1:numel(parms)
parm = parms(iParm);
parmStr = sprintf("%s %s", regexprep(string(parm.getType.getName), 'java.lang\.', ''), ...
string(parm.getName));
parmsStrs(iParm) = parmStr;
end
out = "(" + strjoin(parmsStrs, ", ") + ")";
end
function prettyprintFields(flds)
c = cell(0, 3);
for iFld = 1:numel(flds)
fld = flds(iFld);
mods = decodeModifiers(fld.getModifiers);
typeName = regexprep(string(fld.getType.getName), 'java.lang\.', '');
c = [c; {mods, typeName, string(fld.getName)}];
end
t = cell2table(c, 'VariableNames',{'Modifiers','Type','Name'});
t = sortrows(t, {'Name'});
disp(categoricalizeTable(t));
end
function prettyprintCtors(ctors)
c = cell(0, 3);
for iCtor = 1:numel(ctors)
ctor = ctors(iCtor);
mods = decodeModifiers(ctor.getModifiers);
name = regexprep(string(ctor.getName), '.*\.', '');
c = [c; {mods, name, parms2str(ctor.getParameters)}];
end
t = cell2table(c, 'VariableNames',{'Modifiers','Name','Parameters'});
t = sortrows(t, {'Name', 'Parameters'});
disp(categoricalizeTable(t));
end
function prettyprintMethods(meths)
c = cell(0, 3);
for iMeth = 1:numel(meths)
meth = meths(iMeth);
mods = decodeModifiers(meth.getModifiers);
name = regexprep(string(meth.getName), '.*\.', '');
c = [c; {mods, name, parms2str(meth.getParameters)}];
end
t = cell2table(c, 'VariableNames',{'Modifiers','Name','Parameters'});
t = sortrows(t, {'Name', 'Parameters'});
disp(categoricalizeTable(t));
end
fprintf('\nFields:\n');
prettyprintFields(klass.getFields);
fprintf('\nDeclared Fields:\n');
prettyprintFields(klass.getDeclaredFields);
fprintf('\nConstructors:\n');
prettyprintCtors(klass.getConstructors);
fprintf('\nDeclared Constructors:\n');
prettyprintCtors(klass.getDeclaredConstructors);
fprintf('\nMethods:\n');
prettyprintMethods(klass.getMethods);
fprintf('\nDeclared Methods:\n');
prettyprintMethods(klass.getDeclaredMethods);
sup = klass.getSuperclass;
while string(sup.getName) ~= "java.lang.Object"
fprintf('\nDeclared Methods (%s):\n', string(sup.getName));
prettyprintMethods(sup.getDeclaredMethods);
fprintf('\nDeclared Fields (%s):\n', string(sup.getName));
prettyprintFields(sup.getDeclaredFields);
sup = sup.getSuperclass;
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!