Multiple objects sharing a context menu?
Ältere Kommentare anzeigen
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 Kommentare
Adam
am 3 Aug. 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
am 3 Aug. 2015
Niko
am 21 Aug. 2015
Adam
am 21 Aug. 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
am 21 Aug. 2015
Adam
am 21 Aug. 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.
Akzeptierte Antwort
Weitere Antworten (3)
Phil
am 4 Okt. 2016
4 Stimmen
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 Kommentare
Kristoffer Walker
am 5 Nov. 2018
This is a much better answer. Thanks Phil! What I did was this in my callback:
ax = gco;
Kris
Kristoffer Walker
am 21 Okt. 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.
래충 강
am 17 Dez. 2021
2 Stimmen
get(app.UIFigure, 'CurrentObject')
It tell you which object called the context menu
1 Kommentar
Rina Blomberg
am 3 Feb. 2023
Bearbeitet: Rina Blomberg
am 3 Feb. 2023
Thank you!! Exactly the solution I needed.
Walter Roberson
am 3 Aug. 2015
0 Stimmen
You could Label or Tag them differently; http://www.mathworks.com/help/matlab/ref/uicontextmenu.html
3 Kommentare
Niko
am 3 Aug. 2015
Walter Roberson
am 3 Aug. 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
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!