创建一个下拉菜单(Pop-Up Menu)
注:value值和String中的每一行一一对应,可以尝试把value值改成2或3,会发现下拉菜单会变成cos(x)或者sin(x)+cos(x)。
创建一个坐标系。
编写下拉菜单的回调函数。
代码如下,解释除了下图外其余请参考代码里的注释:
% --- Executes on selection change in kj1.
function kj1_Callback(hObject, eventdata, handles)
% hObject handle to kj1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns kj1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from kj1
val = get(handles.kj1,'value'); %获取下拉菜单的value值
x = 0:0.01:2*pi; % 定义函数的自变量的范围
axes(handles.axes1); %将GUI中的坐标系置为当前
switch val %根据下拉菜单的选项完成相应的操作
case 1
y=sin(x);
plot(x,y);
case 2
y=cos(x);
plot(x,y);
case 3
y=sin(x)+cos(x);
plot(x,y);
end