Assigning a context menu to a UIAxes
조회 수: 14 (최근 30일)
이전 댓글 표시
I am using appdesigner with Matlab 2020b.
I have a UIAxes in the main window, and I would like to assign a context menu to that axis.
I have implemented the context menu, and I can attach it to the 'panel' that contains the UIAxes. The axis functions correctly when I right-click on the panel around the UIAxes.
But I cannot attach the context menu to the UIAxes itself. The menu only appears when I right-click on the panel, not on the UIAxes.
The context menu is assigned to the UIAxes (see attached PNG file).
Brian Wandell
wandell@stanford.edu
(lmperry@stanford.edu is the name of my lab manager. I would appreciate if you could make sure the message is also sent to my account).
댓글 수: 0
채택된 답변
Mario Malic
2020년 11월 7일
Hello Brian,
You can attach Context Menu component to the UIAxes component by drag & drop, from the Component Browser, or from the Context Menus (if it's already made).
If you're creating menus programmatically, then you can assign them to UIAxes by ContextMenu property.
댓글 수: 8
David Cardinal
2020년 11월 9일
Mario -- Thanks again. We're getting closer, but I still run into an issue with the latest version. The WindowButtonDown callback fires correctly, the if statement works, and if the UIAxes is empty, the open() command shows the ContextMenu. But once I load an image into the UIAxes, I don't see the ContextMenu display. The open() command still executes, it just doesn't have any effect (at least none that I can see). Thanks, as always, for any help, or if there is something else we need to do! -- David (& Brian)
Mario Malic
2020년 11월 9일
편집: Mario Malic
2020년 11월 9일
I noticed your app.UIAxes has 5 children, if they are different types, and lay on top of each other, than it might be a problem for us to select image object.
You can see in command window which object gets selected by writing this in WindowsButtonDown callback.
event.Source.CurrentObject
This might be the solution
function UIFigureWindowButtonDown(app, event)
%Create if statement that determines if the user clicked on the
%line of the top UIAxes. If they didn't, do nothing
event.Source.CurrentObject
if ~isempty(event.Source.CurrentObject) && isequal(event.Source.SelectionType, 'alt') % valid selection object&right click
if isequal(event.Source.CurrentObject.Type,'image') || isequal(event.Source.CurrentObject.Parent, 'app.UIAxes') % image obj or parent
open(app.ContextMenu, 250, 250);
end
end
end
추가 답변 (2개)
Brian Wandell
2020년 11월 9일
댓글 수: 1
Mario Malic
2020년 11월 9일
If you click on an image, you would be clicking on one of the children which is most probably child of UIAxes.
if isa(event.Source,'matlab.ui.Figure') % this works for UIFigure and figure - unnecessary
if isa(event.Source.CurrentObject,'matlab.graphics.primitive.Image') % same as below?
if isequal(event.Source.CurrentObject.Type,'image') %
% open(app.ContextMenu, 250, 250);
open(app.imageMenu,250, 250);
Function open, opens context menu https://www.mathworks.com/help/matlab/ref/matlab.ui.container.contextmenu.open.html
You also need to implement a right click, see my last comment.
If you have only one axes that you want to do something with, you can try with only selection type.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!