1、新建GUI編程
一、命令行窗口輸入 guide會出來以下界面,能夠新建空白GUI,也能夠打開已有GUI數組
二、經過工具欄新建app
2、數據傳遞例子編輯器
一、添加輸入框按鈕,設置尺寸大小,內容,格式,標籤ide
二、複製輸入框按鈕,獲得輸出框按鈕函數
三、轉換按鈕工具
(1)添加按鈕ui
注意格式是pushbuttonthis
(2)添加回調函數 spa
(3)運行
三、界面可調
四、滾動條
(1)插入滑動條,設置
(2)靜態文本框
(3)可編輯文本
(4)回調函數
打開滑動條的回調函數
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)
val=get(handles.slider1,'Value');%獲得slider1,屬性爲‘Value’的值
set(handles.edit1,'String',num2str(val));%設置edit1屬性爲‘String’的值
加上最後兩句,便可。
四、單選按鈕
(1)添加單選按鈕,設置
選擇了該按鈕,'Value'值爲最大值,不然爲最小值
(2)添加可編輯文本
(3)添加回調函數
function radiobutton1_Callback(hObject, eventdata, handles) % hObject handle to radiobutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=get(handles.radiobutton1,'Value');%獲得slider1,屬性爲‘Value’的值 set(handles.edit1,'String',num2str(val));%設置edit1屬性爲‘Strin % Hint: get(hObject,'Value') returns toggle state of radiobutton1
(5)更改最大最小值及設置
五、複選框
(1)添加複選框,設置
(2)添加可編輯文本框(如上)
(3)回調函數
function checkbox1_Callback(hObject, eventdata, handles) % hObject handle to checkbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=get(handles.checkbox1,'Value');%獲得slider1,屬性爲‘Value’的值 set(handles.edit2,'String',num2str(val));%設置edit1屬性爲‘Strin % Hint: get(hObject,'Value') returns toggle state of checkbox1
六、切換函數
(1)添加切換按鈕
(2)添加文本框
(3)添加回調函數
function togglebutton2_Callback(hObject, eventdata, handles) % hObject handle to togglebutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=get(handles.togglebutton2,'Value');%獲得slider1,屬性爲‘Value’的值 set(handles.edit3,'String',num2str(val));%設置edit1屬性爲‘Strin % Hint: get(hObject,'Value') returns toggle state of togglebutton2
七、按鈕組
(1)添加按鈕組控件,設置顯示文字及大小
(2)添加三個單選按鈕,更改顯示內容和大小
運行一下,以下圖
同一時刻只能選擇一個按鈕,這就是按鈕組的特色
(3)添加座標軸
(4)添加SelectionChangedFcn函數
function uibuttongroup1_SelectionChangedFcn(hObject, eventdata, handles) % hObject handle to the selected object in uibuttongroup1 % 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;%定義x軸座標 axes(handles.axes1);%選中你要畫圖的座標系 current_selection=get(eventdata.NewValue,'tag'); %困擾了一天的問題終於解決了 %原來 是Tag 返回值是字符串類型 % case後的選擇條件要加引號 switch current_selection case 'radiobutton1' y1=sin(x); plot(x,y1); grid on title('sin(x)') case 'radiobutton2' y2=cos(x); plot(x,y2); grid on title('cos(x)') case 'radiobutton3' y3=sin(x)+cos(x); plot(x,y3); grid on title('sin(x)+cos(x)') end
八、彈出式菜單
(1)添加彈出式菜單
Value 隨着選擇不一樣分別爲1,2,3
(2)添加回掉函數
function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=get(handles.popupmenu1,'Value')%不加分號,將結果顯示
選擇不一樣的函數,返回不一樣的值
(3)添加座標軸
(4)添加回調函數
function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=get(handles.popupmenu1,'Value'); x=0:0.01:2*pi;%定義x軸座標 axes(handles.axes1);%選中你要畫圖的座標系 switch val case 1 y1=sin(x); plot(x,y1); grid on title('sin(x)') case 2 y2=cos(x); plot(x,y2); grid on title('cos(x)') case 3 y3=sin(x)+cos(x);
handles.h=plot(x,y3);%建立句柄
set(handles.h,'Color',rand(1,3));%設置顏色
title('sin(x)+cos(x)')
end
九、listbox控件
(1)建立listbox控件
Value的值和list內容一一對應。
(2)添加按鈕
(3)添加可編輯文本
(4)按鈕函數添加回調函數
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.listbox1,'String');%獲得listbox1中String中的字符串,列表格式 index_x=get(handles.listbox1,'Value');%列表的下標 set(handles.edit1,'String',str(index_x));%將字符串顯示在可編輯文本框中
y軸的回調函數同理。
(5)CreatFcn函數(listbox將參數函數化)
function listbox1_CreateFcn(hObject, eventdata, handles) % hObject handle to listbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end t=0:0.01:2*pi; canshu1=t; canshu2=sin(t); canshu3=cos(t); canshu4=sin(t)+cos(t);
建立新的結構體,保存handles結構體
cl=[canshu1;canshu2;canshu3;canshu4];%建立新的結構體,注意必定使用分號,不然獲得的是一串數字。或者使用元胞格式,可是注意調試數據獲取方式 handles.cl=cl; guidata(hObject,handles);%更新handles結構體
(6)回調函數中,選擇的x,y軸參數賦值
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.listbox1,'String');%獲得listbox1中String中的字符串,列表格式 index_x=get(handles.listbox1,'Value');%列表的下標 set(handles.edit1,'String',str(index_x));%將字符串顯示在可編輯文本框中 x=handles.cl(index_x,:);%選擇的x軸的數值 handles.x=x;%必須定義新的handles,不然無法傳遞到下一個函數 guidata(hObject,handles)
y軸同理
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) str=get(handles.listbox1,'String'); index_y=get(handles.listbox1,'Value'); set(handles.edit2,'String',str(index_y)); y=handles.cl(index_y,:); handles.y=y; guidata(hObject,handles)
(7)建立繪圖按鈕和座標軸,而且添加繪圖按鈕回調函數
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) axes(handles.axes2);%選中你要畫圖的座標系 plot(handles.x,handles.y); grid on axis equal
元胞數組形式的所有程序
function varargout = list_1(varargin) % LIST_1 MATLAB code for list_1.fig % LIST_1, by itself, creates a new LIST_1 or raises the existing % singleton*. % % H = LIST_1 returns the handle to a new LIST_1 or the handle to % the existing singleton*. % % LIST_1('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in LIST_1.M with the given input arguments. % % LIST_1('Property','Value',...) creates a new LIST_1 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before list_1_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to list_1_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help list_1 % Last Modified by GUIDE v2.5 23-Nov-2017 00:35:23 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @list_1_OpeningFcn, ... 'gui_OutputFcn', @list_1_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before list_1 is made visible. function list_1_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to list_1 (see VARARGIN) % Choose default command line output for list_1 handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes list_1 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = list_1_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on selection change in listbox1. function listbox1_Callback(hObject, eventdata, handles) % hObject handle to listbox1 (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 listbox1 contents as cell array % contents{get(hObject,'Value')} returns selected item from listbox1 % --- Executes during object creation, after setting all properties. function listbox1_CreateFcn(hObject, eventdata, handles) % hObject handle to listbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end t=0:0.01:2*pi; canshu1=t; canshu2=sin(t); canshu3=cos(t); canshu4=sin(t)+cos(t); cl={canshu1,canshu2,canshu3,canshu4};%元胞數組建立新的結構體 handles.cl=cl; guidata(hObject,handles)%更新handles結構體 % --- Executes on button press in pushbutton1. 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.listbox1,'String');%獲得listbox1中String中的字符串,列表格式 index_x=get(handles.listbox1,'Value');%列表的下標 set(handles.edit1,'String',str(index_x));%將字符串顯示在可編輯文本框中 x=handles.cl{index_x};%選擇的x軸的數值 handles.x=x;%handles化 guidata(hObject,handles) % --- Executes on button press in pushbutton2. 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) str=get(handles.listbox1,'String'); index_y=get(handles.listbox1,'Value'); set(handles.edit2,'String',str(index_y)); y=handles.cl{index_y};%注意使用花括號 handles.y=y;%建立新的元胞數組傳遞數值 guidata(hObject,handles) function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (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,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (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,'String') returns contents of edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double % --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton3. 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) axes(handles.axes2);%選中你要畫圖的座標系 plot(handles.x,handles.y); grid on axis equal
注意整個實例是利用handles來傳遞全局變量。
字符的一開始參數化,編程很好的思想。
十、菜單編輯
(1)建立菜單編輯器
(2)建立座標軸
(3)添加回調函數
function sin_x_Callback(hObject, eventdata, handles) % hObject handle to sin_x (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; axes(handles.axes1); handles.h=plot(x,sin(x));%建立畫圖句柄 grid on title('正弦曲線') guidata(hObject,handles)%保存句柄 % -------------------------------------------------------------------- function cos_x_Callback(hObject, eventdata, handles) % hObject handle to cos_x (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; axes(handles.axes1); handles.h=plot(x,cos(x));%建立畫圖句柄 grid on title('餘弦曲線') guidata(hObject,handles)%保存句柄
(4)添加上下文菜單
更改座標軸設置
(5)添加顏色、線寬回調函數
% -------------------------------------------------------------------- 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','red')%設置紅色 % -------------------------------------------------------------------- function yellow_Callback(hObject, eventdata, handles) % hObject handle to yellow (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','y') % -------------------------------------------------------------------- function grean_Callback(hObject, eventdata, handles) % hObject handle to grean (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 linewidth_1_Callback(hObject, eventdata, handles) % hObject handle to linewidth_1 (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 linewidth_2_Callback(hObject, eventdata, handles) % hObject handle to linewidth_2 (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',4)%線寬