Multiple Uses of UIMENU

조회 수: 21 (최근 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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by