Help with pop up menu in a GUI interface

3 次查看(过去 30 天)
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

采纳的回答

Geoff Hayes
Geoff Hayes 2015-1-28
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!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by