Matlab界面设计入门

来源:互联网 发布:专业电脑视频剪辑软件 编辑:程序博客网 时间:2024/04/27 21:38

1.文本编辑框+按钮
数据传递
pushbutton的callback代码:

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)str=get(handles.edit1,'String');set(handles.edit2,'String',str);

2.滚动条显示值
滚动条同步显示
滚动条后台callback代码(实践由滚动条产生触发):

function slider1_Callback(hObject, eventdata, handles)% hObject    handle to slider1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'Value') returns position of slider%        get(hObject,'Min') and get(hObject,'Max') to determine range of slidervar=get(handles.slider1,'value');%value 属性哦set(handles.edit1,'String',num2str(var));

3.单选框+复选框+togglebutton简单示例
切换现实上下限的值
三个控件的后台callback:

function rb_Callback(hObject, eventdata, handles)% hObject    handle to rb (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of rbvar=get(handles.rb,'value');set(handles.edit1,'string',num2str(var));% --- Executes on button press in ck.function ck_Callback(hObject, eventdata, handles)% hObject    handle to ck (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)var=get(handles.ck,'value');set(handles.edit2,'string',num2str(var));% Hint: get(hObject,'Value') returns toggle state of ck% --- Executes on button press in tb.function tb_Callback(hObject, eventdata, handles)% hObject    handle to tb (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of tbvar=get(handles.tb,'value');set(handles.edit3,'string',num2str(var));

4.选择描绘不同三角函数曲线
buttonGroup的使用
将三个radio button放在一个button group中。
buttonGroup的后台SelectionChangeFcn:

function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)% hObject    handle to the selected object in uipanel1 % eventdata  structure with the following fields (see UIBUTTONGROUP)%   EventName: string 'SelectionChanged' (read only)%   OldValue: handle of the previously selected object or empty if none was selected%   NewValue: handle of the currently selected object% handles    structure with handles and user data (see GUIDATA)x=0:0.01:2*pi;%x轴的范围0-2picurrent_Obj=get(eventdata.NewValue,'Tag');axes(handles.axes1);switch current_Obj    case 'rb1'      y=sin(x);      plot(x,y);    case 'rb2'        y=cos(x);        plot(x,y);    case 'rb3'        y=sin(x)+cos(x);        plot(x,y);end

5.下拉选择功能菜单
下拉框选择函数功能
pop up menu的callback代码:

function ppm_Callback(hObject, eventdata, handles)% hObject    handle to ppm (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 ppm contents as cell array%        contents{get(hObject,'Value')} returns selected item from ppmvar=get(handles.ppm,'value');x=0:0.01:2*pi;axes(handles.axes1);switch var    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
0 0