Multiple Uses of UIMENU

14 次查看(过去 30 天)
Jason
Jason 2024-3-4
评论: Voss 2024-3-5
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;

采纳的回答

Voss
Voss 2024-3-4
编辑:Voss 2024-3-4
"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:
  • 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)
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 个评论
Jason
Jason 2024-3-5
thats awesome, thankyou very much for your effort!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by