Assigning a context menu to a UIAxes

40 Ansichten (letzte 30 Tage)
Brian Wandell
Brian Wandell am 7 Nov. 2020
Beantwortet: Brian Wandell am 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).

Akzeptierte Antwort

Mario Malic
Mario Malic am 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 Kommentare
David Cardinal
David Cardinal am 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
Mario Malic am 9 Nov. 2020
Bearbeitet: Mario Malic am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Brian Wandell
Brian Wandell am 9 Nov. 2020
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 Kommentar
Mario Malic
Mario Malic am 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);
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.

Melden Sie sich an, um zu kommentieren.


Brian Wandell
Brian Wandell am 10 Nov. 2020
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.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by