Assigning a context menu to a UIAxes
36 views (last 30 days)
Show older comments
Brian Wandell
on 7 Nov 2020
Answered: Brian Wandell
on 10 Nov 2020
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 Comments
Accepted Answer
Mario Malic
on 7 Nov 2020
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 Comments
David Cardinal
on 9 Nov 2020
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
on 9 Nov 2020
Edited: Mario Malic
on 9 Nov 2020
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
More Answers (2)
Brian Wandell
on 9 Nov 2020
1 Comment
Mario Malic
on 9 Nov 2020
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.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!