Multiple objects sharing a context menu?
Mostrar comentarios más antiguos
Hi all,
I'm trying to have multiple objects in the same figure share a common context menu. In the menu callback, is there a way to tell which one of the objects initiated the callback (i.e. was right-clicked on)? Or do I have to create a separate contextmenu object for each one of them?
Thanks!
Niko
6 comentarios
Adam
el 3 de Ag. de 2015
Does the source (1st argument) of the callback not tell you? I can't remember off-hand what the source returns for a context menu, but usually it is the object that triggered the callback.
Niko
el 3 de Ag. de 2015
Niko
el 21 de Ag. de 2015
Adam
el 21 de Ag. de 2015
I would probably do it by creating a class to wrap up the context menu, add any extra information required to its callback and just create an instance of that class for each place where it is needed, but if you aren't familiar with Matlab OOP that would be quite a steep learning curve possibly for this example.
Niko
el 21 de Ag. de 2015
Adam
el 21 de Ag. de 2015
You can set the callback and when you do you can pass in other arguments than just those that come automatically. If your context menu is embedded in a class you only need to define the context menu once, but the class can also contain e.g. a line object handle or whatever else it needs to allow it to link together the context menu and the thing it is being attached to.
Then you would create and instance of your class for each context menu you want, each instance of the class would link to a different graphics object which it would then pass into the context menu's callback to allow you to get the context specific information.
Respuesta aceptada
Más respuestas (3)
Phil
el 4 de Oct. de 2016
4 votos
There is a simpler answer. You can use "gco" in callbacks called by the context menu to get the handle of the current graphics object that was clicked. Thus the menu can be shared between objects without difficulty and still have different behavior for each object. For example, to delete the specific object clicked, use the callback "@(~,~)delete(gco)".
4 comentarios
Kristoffer Walker
el 5 de Nov. de 2018
This is a much better answer. Thanks Phil! What I did was this in my callback:
ax = gco;
Kris
Kristoffer Walker
el 21 de Oct. de 2020
Folks,
This does not work with AppDesigner. Is there a similar simple solution for AppDesigner? This is a fundamental need for developers. Many thanks to all of you who contribute to knowledge sharing on this forum!!
Kris
@Kristoffer Walker, that's because the HandleVisibility of uifigures is set to 'off' by default which prevents getting access to the figure handle or any object within the figure.
solution 1: set the uifigure's HandleVisibility to 'callback' or 'on'--(not recommended)
solution 3 (best): instead of relying on the object being 'current', pass the object's handles into the function or use a pre-defined obj handle in the function definition.
래충 강
el 17 de Dic. de 2021
2 votos
get(app.UIFigure, 'CurrentObject')
It tell you which object called the context menu
1 comentario
Rina Blomberg
el 3 de Feb. de 2023
Editada: Rina Blomberg
el 3 de Feb. de 2023
Thank you!! Exactly the solution I needed.
Walter Roberson
el 3 de Ag. de 2015
0 votos
You could Label or Tag them differently; http://www.mathworks.com/help/matlab/ref/uicontextmenu.html
3 comentarios
Niko
el 3 de Ag. de 2015
Walter Roberson
el 3 de Ag. de 2015
No, notice that the single UiContextMenu callback is used in the example there and that it pulls information out of the Label (in that case) to decide what to do. It is not well-documented in the description of uicontextmenu what the source object is (the first parameter passed in) but you can deduce from the example that it is the menu object rather than the uicontextmenu object
Categorías
Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!