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