- have the context menu contain all menu items you need for all uitables
- when right-clicking on a component, the component's ButtonDownFcn (if defined) executes before the context menu appears, so any logic you need to update the menu items for the specific component can be in the ButtonDownFcn
- in this case, put code in the ButtonDownFcn to set the "extra" menu item visible if right-clicking on the one specific table and invisible if right-clicking on any other table (the first input to the ButtonDownFcn is the clicked component, so you can use that to know which uitable was clicked on)
Multiple Uses of UIMENU
6 views (last 30 days)
Show older comments
Hello, I have just discovered the beauty of uimenu's and think there brilliant as can unclog a GUI since less button!
However, Im not too sure about reusing the code, for example if I have 3 UITables as below (in reality I have about 10), there seems to be a lot of duplicity. Is there a more elegant way to ahcieve this
Also, what if for one of the tables I wanted the menu to e.g. have one more item thats not in the other menu's?
function startupFcn(app)
fig= app.UIFigure;
cm = uicontextmenu(fig);
m = uimenu(cm,"Text","Delete Row");
m1 = uimenu(cm,"Text","Paste Data");
%m.Text = "Delete Row";
%m1.Text = "Paste Data";
tbl=app.UITable1;
tbl.ContextMenu = cm;
m.MenuSelectedFcn = @app.deleteRow; %create callback. IMPORTANT: You must use app.****
m1.MenuSelectedFcn = @(src,event)app.TablePasteData(src,event,tbl);
cm.ContextMenuOpeningFcn = @(src,event)app.toggleVisibility(src,event,m);
%Apply row delete option to other UITABLE2
cm2 = uicontextmenu(fig);
m = uimenu(cm2,"Text","Delete Row");
m1 = uimenu(cm2,"Text","Paste Data");
tbl=app.UITable2;
tbl.ContextMenu = cm2;
m.MenuSelectedFcn = @app.deleteRow; %create callback. IMPORTANT: You must use app.****
m1.MenuSelectedFcn = @(src,event)app.TablePasteData(src,event,tbl);
cm.ContextMenuOpeningFcn = @(src,event)app.toggleVisibility(src,event,m);
%Apply row delete option to other UITABLE3
cm3 = uicontextmenu(fig);
m = uimenu(cm3,"Text","Delete Row");
m1 = uimenu(cm3,"Text","Paste Data");
tbl=app.UITable3;
tbl.ContextMenu = cm3;
m1.MenuSelectedFcn = @(src,event)app.TablePasteData(src,event,tbl);
and here is one of my functions for example
function deleteRow(app,src,event)
tbl = event.ContextObject;
row = event.InteractionInformation.Row;
tbl.Data(row,:) = [];
end
(and do I need the src & event in the function argument if instead I call it like this:
m.MenuSelectedFcn = @(src,event)app.deleteRow
instead of
m.MenuSelectedFcn = @app.deleteRow;
0 Comments
Accepted Answer
Voss
on 4 Mar 2024
Edited: Voss
on 4 Mar 2024
"there seems to be a lot of duplicity. Is there a more elegant way to ahcieve this"
Multiple components (e.g., uitables) can share the same context menu.
"what if for one of the tables I wanted the menu to e.g. have one more item thats not in the other menu's?"
If all uitables share a context menu:
If each table has its own context menu: make one context menu have an additional menu item.
"and do I need the src & event in the function argument"
Depends on how the MenuSelectedFcn works.
16 Comments
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!