matlab GUI (two)

1. 下拉菜单的使用方法

  

   我们在 “弹出框” 的string中写入三行: sin(x), cos(x), sin(x)+cos(x)。value分别为1,2,3。

  在 对应的回调函数中加入:

var=get(handles.kg1,'value')

  我们选择对应的string即可修改对应的value。

 

   补充完整:

function kg1_Callback(hObject, eventdata, handles)
% hObject    handle to kg1 (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 kg1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from kg1
var=get(handles.kg1,'value');
x=0:0.01:2*pi;
axes(handles.axes1);
switch var
    case 1
        y=sin(x);
        plot(x,y,'b')
    case 2
        y=cos(x);
        plot(x,y,'c')
    case 3
        y=sin(x)+cos(x);
        plot(x,y,'g');
end

 2. ListBox的使用

  我们首先创建控件:listbox,button和edit。如下:

function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selected_indexed=get(handles.listbox1,'value');
str=get(handles.listbox1,'string');
set(handles.edit2,'string',str{selected_indexed});
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selected_indexed=get(handles.listbox1,'value');
str=get(handles.listbox1,'string');
set(handles.edit1,'string',str{selected_indexed});

   当然了,我们也可以加上一个 坐标系:

   添加对应的回调函数:

function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=get(handles.edit1,'string');
y=get(handles.edit2,'string');
a=0:0.01:2*pi;
axes(handles.axes1);
switch x
    case 't'
        xx=a;
    case 'sin(t)'
        xx=sin(a);
    case 'cos(t)'
        xx=cos(a);
    case 'sin(t)+cos(t)'
        xx=sin(a)+cos(a);
end
switch y
    case 't'
        yy=a;
    case 'sin(t)'
        yy=sin(a);
    case 'cos(t)'
        yy=cos(a);
    case 'sin(t)+cos(t)'
        yy=sin(a)+cos(a);
end
plot(xx,yy,'g')

 3. 菜单控件的使用方法

  首先我们在 在figure中创建一个坐标系:

   然后我们打开导航栏的菜单选项:

   在对其回调函数进行编写:

function sinx_Callback(hObject, eventdata, handles)
% hObject    handle to sinx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=0:0.01:2*pi;
y=sin(x);
axes(handles.axes1)
h=plot(x,y,'g');
handles.h=h;
guidata(hObject,handles);

% --------------------------------------------------------------------
function cosx_Callback(hObject, eventdata, handles)
% hObject    handle to cosx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=0:0.01:2*pi;
y=cos(x);
axes(handles.axes1)
h=plot(x,y,'b');
handles.h=h;
guidata(hObject,handles);

   接下俩我们再绘制 上下文菜单:

   再分别书写 颜色 和 线宽的回调函数:

function red_Callback(hObject, eventdata, handles)
% hObject    handle to red (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.h,'color','r')

% --------------------------------------------------------------------
function green_Callback(hObject, eventdata, handles)
% hObject    handle to green (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.h,'color','g')

% --------------------------------------------------------------------
function black_Callback(hObject, eventdata, handles)
% hObject    handle to black (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.h,'color','k')
function width1_Callback(hObject, eventdata, handles)
% hObject    handle to width1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.h,'LineWidth',1)


% --------------------------------------------------------------------
function width2_Callback(hObject, eventdata, handles)
% hObject    handle to width2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.h,'LineWidth',2)



% --------------------------------------------------------------------
function width3_Callback(hObject, eventdata, handles)
% hObject    handle to width3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.h,'LineWidth',3)

 

posted @ 2021-04-08 20:41  为红颜  阅读(157)  评论(0编辑  收藏  举报