Assigning a context menu to a UIAxes

39 次查看(过去 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).

采纳的回答

Mario Malic
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
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
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
Brian Wandell 2020-11-9
Thanks. Your suggestions have been very helpful. I evolved to a variant of your code that brings up the context menu only when clicking on the image window.
It would be nice to have fewer if() checks. And I have no idea what the numerical parameters to open(XXX, 250,250) do. But so far so good.
if isa(event.Source,'matlab.ui.Figure')
if isa(event.Source.CurrentObject,'matlab.graphics.primitive.Image')
if isequal(event.Source.CurrentObject.Type,'image')
% open(app.ContextMenu, 250, 250);
open(app.imageMenu,250, 250);
end
end
end
This work around is helpful. I consider the situation with the Matlab product, however, to be a bug. How about you?
Brian
  1 个评论
Mario Malic
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);
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.

请先登录,再进行评论。


Brian Wandell
Brian Wandell 2020-11-10
Thanks for the comments. But, when I clicked around the different panels and objects with only the 'Type' check, some event.Source cases did not include the 'CurrentObject' slot. That's why I walked down the tests term by term.
I will experiment some more, but this is now working. Again - your help is much appreciated.
For anyone else reading this - particularly someone from Mathworks - I consider the Matlab 2020b to have a bug with respect to attaching the context menu to the UIAxes. What is described here is a work-around.

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by