Multiple objects sharing a context menu?
Mostra commenti meno recenti
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 Commenti
Adam
il 3 Ago 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
il 3 Ago 2015
Niko
il 21 Ago 2015
Adam
il 21 Ago 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
il 21 Ago 2015
Adam
il 21 Ago 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.
Risposta accettata
Più risposte (3)
Phil
il 4 Ott 2016
4 voti
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 Commenti
Kristoffer Walker
il 5 Nov 2018
This is a much better answer. Thanks Phil! What I did was this in my callback:
ax = gco;
Kris
Kristoffer Walker
il 21 Ott 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.
래충 강
il 17 Dic 2021
2 voti
get(app.UIFigure, 'CurrentObject')
It tell you which object called the context menu
1 Commento
Rina Blomberg
il 3 Feb 2023
Modificato: Rina Blomberg
il 3 Feb 2023
Thank you!! Exactly the solution I needed.
Walter Roberson
il 3 Ago 2015
0 voti
You could Label or Tag them differently; http://www.mathworks.com/help/matlab/ref/uicontextmenu.html
3 Commenti
Niko
il 3 Ago 2015
Walter Roberson
il 3 Ago 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
Categorie
Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!