Multiple Uses of UIMENU

19 ビュー (過去 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!
Voss
Voss 2024 年 3 月 5 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by