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 slider
var=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 rb
var=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 tb
var=get(handles.tb,'value');
set(handles.edit3,'string',num2str(var));
4.选择描绘不同三角函数曲线
将三个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-2pi
current_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 ppm
var=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