Help with pop up menu in a GUI interface

3 visualizzazioni (ultimi 30 giorni)
I have to use for the first time a popup menu in a GUI interface. I know I can switch two or more options with it; for example, if 'plot1' and 'plot2' are the two cases, I can decide which one I want to plot, with this function:
function popupmenu1_Callback(hObject, eventdata, handles)
str = get(hObject, 'String');
val = get(hObject,'Value');
% Set current data to the selected data set.
switch str{val};
case 'plot1'
axes(handles.plot)
plot(x1,y1);
case 'plot2'
axes(handles.plot)
plot(x2,y2);
end
guidata(hObject,handles)
but what if I want to use a push button whose action depends on the string appearing in the popup menu? For example, if I have a pushbutton 'Linear fit' whose callback function makes a linear fit of the plot, how can I distinguish here the two cases of the popup menu? Thanks

Risposta accettata

Geoff Hayes
Geoff Hayes il 28 Gen 2015
aurc89 - in your push button callback, use the handles structure to refer back to your popup menu object named popupmenu1. For example,
function pushbutton1_Callback(hObject,eventdata,handles)
str = get(handles.popupmenu1, 'String');
val = get(handles.popupmenu1,'Value');
% etc.
Note that you may not need to get the string values back and can just use the integer val instead. (Also, in your popup menu callback, there is no need for the guidata(hObject,handles) because you haven't updated the handles structure..unless you haven't shown all of your code.) Try the above and see what happens!

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by