How to relate pushbutton with popup menu while plotting multiple graphs on a same figure?

I want to know that how can I relate push button with popup menu in GUI. I am trying to plot sine, cos and tangent graph on a same figure using one push button and a popup menu. So whenever I select sine from popup menu, the graph of sine must be displayed and same goes with cos and tangent. How can I do this? How should I relate push button with popup menu? Sorry, I do not know how to use handles.

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 21 Okt. 2016
Bearbeitet: Geoff Hayes am 21 Okt. 2016
Arsala - if you are using GUIDE, then use the handles structure that is passed into the push button callback to determine which trig function has been selected. For example, if your push button is named pushbutton1 and your popup menu is named popupmenu1 then you could do
function pushbutton1_Callback(hObject, eventdata, handles)
% get the index of the selected item from the menu
selectedItemIdx = get(handles.popupmenu1,'Value');
if selectedItemIdx == 1
% use sine
elseif selectedItemIdx == 2
% use cos
elseif selectedItemIdx == 3
% use tan
end
Try the above and see what happens!

6 Kommentare

Thank you Geoff for your answer. It is really helpful. But tell me what do I have to write in my popupmenu1 function?
Arsala - the code in your pop up menu would be very similar to that from above (and so should/could be put in a helper function to avoid duplication)
function popupmenu1_Callback(hObject, eventdata, handles)
% get the index of the selected item from the menu
selectedItemIdx = get(hObject,'Value');
if selectedItemIdx == 1
% use sine
elseif selectedItemIdx == 2
% use cos
elseif selectedItemIdx == 3
% use tan
end
Thank you Geoff. Can you please tell me how to use handle structure?
Arsala - there is no reason to use the handles structure in the above code unless you are trying to access some other control or attribute that has been set within handles.
If you have R2014b or later release, you can use the more modern and convenient OOP syntax:
selectedItemIdx = handles.popupmenu1.Value;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects 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!

Translated by