gusucode.com > matlab界面设计的一些资料笔记,可以做为初学者参考用 > GUI制作中文资料/zhuchengfen_window.m
function varargout = zhuchengfen_window(varargin) % ZHUCHENGFEN_WINDOW M-file for zhuchengfen_window.fig % ZHUCHENGFEN_WINDOW, by itself, creates a new ZHUCHENGFEN_WINDOW or raises the existing % singleton*. % % H = ZHUCHENGFEN_WINDOW returns the handle to a new ZHUCHENGFEN_WINDOW or the handle to % the existing singleton*. % % ZHUCHENGFEN_WINDOW('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in ZHUCHENGFEN_WINDOW.M with the given input arguments. % % ZHUCHENGFEN_WINDOW('Property','Value',...) creates a new ZHUCHENGFEN_WINDOW or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before zhuchengfen_window_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to zhuchengfen_window_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 % Copyright 2002-2003 The MathWorks, Inc. % Edit the above text to modify the response to help zhuchengfen_window % Last Modified by GUIDE v2.5 14-Oct-2004 20:12:07 % Begin initialization code - DO NOT EDIT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %定义全局变量,存储原始矩阵 global YSData; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @zhuchengfen_window_OpeningFcn, ... 'gui_OutputFcn', @zhuchengfen_window_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 zhuchengfen_window is made visible. function zhuchengfen_window_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 zhuchengfen_window (see VARARGIN) % Choose default command line output for zhuchengfen_window handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes zhuchengfen_window wait for user response (see UIRESUME) % uiwait(handles.figure1); %初始化个控件 set(handles.radiobutton5,'Value',1); set(handles.radiobutton8,'Value',1); set(handles.checkbox1,'Value',1); set(handles.checkbox2,'Value',1); set(handles.checkbox3,'Value',1); set(handles.checkbox4,'Value',1); set(handles.checkbox5,'Value',1); set(handles.checkbox6,'Value',1); set(handles.checkbox7,'Value',1); set(handles.checkbox8,'Value',1); set(handles.checkbox9,'Value',1); set(handles.pushbutton2,'Enable','off'); set(handles.edit1,'String','0.8'); % --- Outputs from this function are returned to the command line. function varargout = zhuchengfen_window_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 button press in checkbox1. 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) % Hint: get(hObject,'Value') returns toggle state of checkbox1 %判断'数据标准化'是否被选中,根据情况对下面各单选按钮设置状态 if(get(handles.checkbox1,'Value')==1) set(handles.radiobutton1,'Enable','on'); set(handles.radiobutton2,'Enable','on'); set(handles.radiobutton3,'Enable','on'); set(handles.radiobutton4,'Enable','on'); set(handles.radiobutton5,'Enable','on','Value',1); set(handles.radiobutton6,'Enable','on'); set(handles.radiobutton7,'Enable','on'); end if(get(handles.checkbox1,'Value')==0) set(handles.radiobutton1,'Enable','off','Value',0); set(handles.radiobutton2,'Enable','off','Value',0); set(handles.radiobutton3,'Enable','off','Value',0); set(handles.radiobutton4,'Enable','off','Value',0); set(handles.radiobutton5,'Enable','off','Value',0); set(handles.radiobutton6,'Enable','off','Value',0); set(handles.radiobutton7,'Enable','off','Value',0); end % --- Executes on button press in radiobutton1. 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) % Hint: get(hObject,'Value') returns toggle state of radiobutton1 %当选择‘总和标准化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',1); set(handles.radiobutton2,'Value',0); set(handles.radiobutton3,'Value',0); set(handles.radiobutton4,'Value',0); set(handles.radiobutton5,'Value',0); set(handles.radiobutton6,'Value',0); set(handles.radiobutton7,'Value',0); % --- Executes on button press in radiobutton2. function radiobutton2_Callback(hObject, eventdata, handles) % hObject handle to radiobutton2 (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 radiobutton2 %当选择‘最大值标准化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',0); set(handles.radiobutton2,'Value',1); set(handles.radiobutton3,'Value',0); set(handles.radiobutton4,'Value',0); set(handles.radiobutton5,'Value',0); set(handles.radiobutton6,'Value',0); set(handles.radiobutton7,'Value',0); % --- Executes on button press in radiobutton3. function radiobutton3_Callback(hObject, eventdata, handles) % hObject handle to radiobutton3 (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 radiobutton3 %当选择‘模标准化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',0); set(handles.radiobutton2,'Value',0); set(handles.radiobutton3,'Value',1); set(handles.radiobutton4,'Value',0); set(handles.radiobutton5,'Value',0); set(handles.radiobutton6,'Value',0); set(handles.radiobutton7,'Value',0); % --- Executes on button press in radiobutton4. function radiobutton4_Callback(hObject, eventdata, handles) % hObject handle to radiobutton4 (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 radiobutton4 %当选择‘中心标准化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',0); set(handles.radiobutton2,'Value',0); set(handles.radiobutton3,'Value',0); set(handles.radiobutton4,'Value',1); set(handles.radiobutton5,'Value',0); set(handles.radiobutton6,'Value',0); set(handles.radiobutton7,'Value',0); % --- Executes on button press in radiobutton5. function radiobutton5_Callback(hObject, eventdata, handles) % hObject handle to radiobutton5 (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 radiobutton5 %当选择‘标准差标准化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',0); set(handles.radiobutton2,'Value',0); set(handles.radiobutton3,'Value',0); set(handles.radiobutton4,'Value',0); set(handles.radiobutton5,'Value',1); set(handles.radiobutton6,'Value',0); set(handles.radiobutton7,'Value',0); % --- Executes on button press in radiobutton6. function radiobutton6_Callback(hObject, eventdata, handles) % hObject handle to radiobutton6 (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 radiobutton6 %当选择‘级差标准化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',0); set(handles.radiobutton2,'Value',0); set(handles.radiobutton3,'Value',0); set(handles.radiobutton4,'Value',0); set(handles.radiobutton5,'Value',0); set(handles.radiobutton6,'Value',1); set(handles.radiobutton7,'Value',0); % --- Executes on button press in radiobutton7. function radiobutton7_Callback(hObject, eventdata, handles) % hObject handle to radiobutton7 (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 radiobutton7 %当选择‘级差正规化’时,其他按钮的选中标记被去掉 set(handles.radiobutton1,'Value',0); set(handles.radiobutton2,'Value',0); set(handles.radiobutton3,'Value',0); set(handles.radiobutton4,'Value',0); set(handles.radiobutton5,'Value',0); set(handles.radiobutton6,'Value',0); set(handles.radiobutton7,'Value',1); % --- Executes on button press in radiobutton8. function radiobutton8_Callback(hObject, eventdata, handles) % hObject handle to radiobutton8 (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 radiobutton8 %当选择‘按累积贡献率’时,另一按钮的选中标记被去掉,其后面的编辑框变灰 set(handles.radiobutton8,'Value',1); set(handles.radiobutton9,'Value',0); set(handles.edit1,'Enable','on'); set(handles.edit2,'Enable','off','String',''); % --- Executes on button press in radiobutton9. function radiobutton9_Callback(hObject, eventdata, handles) % hObject handle to radiobutton9 (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 radiobutton9 %当选择‘按主成分个数’时,另一按钮的选中标记被去掉,其后面的编辑框变灰 set(handles.radiobutton8,'Value',0); set(handles.radiobutton9,'Value',1); set(handles.edit1,'Enable','off','String',''); set(handles.edit2,'Enable','on'); 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 set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 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 set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes on button press in checkbox2. function checkbox2_Callback(hObject, eventdata, handles) % hObject handle to checkbox2 (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 checkbox2 % --- Executes on button press in checkbox3. function checkbox3_Callback(hObject, eventdata, handles) % hObject handle to checkbox3 (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 checkbox3 % --- Executes on button press in checkbox4. function checkbox4_Callback(hObject, eventdata, handles) % hObject handle to checkbox4 (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 checkbox4 % --- Executes on button press in checkbox5. function checkbox5_Callback(hObject, eventdata, handles) % hObject handle to checkbox5 (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 checkbox5 % --- Executes on button press in checkbox7. function checkbox7_Callback(hObject, eventdata, handles) % hObject handle to checkbox7 (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 checkbox7 % --- Executes on button press in checkbox7. function checkbox8_Callback(hObject, eventdata, handles) % hObject handle to checkbox7 (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 checkbox7 % --- Executes on button press in checkbox9. function checkbox9_Callback(hObject, eventdata, handles) % hObject handle to checkbox9 (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 checkbox9 % --- Executes on button press in checkbox9. function checkbox10_Callback(hObject, eventdata, handles) % hObject handle to checkbox9 (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 checkbox9 % --- 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) %打开并读取数据文件 %说明YSData是全局变量 global YSData; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [filename,pathname,FILTERINDEX]=uigetfile({'*.txt';'*.dat';'*.*'},'选择数据文件'); if(FILTERINDEX==0) return; end str_filename=[pathname,filename]; fid=fopen(str_filename,'rt'); if(fid==-1) errordlg('Open file error!','Open error'); return; end mn=fscanf(fid,'%f,%f',2); temp=textscan(fid,'%f','returnOnError',0); YSData=vec2mat(temp{1},mn(2)); %只有正确打开数据文件时,才能激活‘开始分析’按钮 set(handles.pushbutton2,'Enable','on'); % --- 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) %进行主成分分析 %说明YSData是全局变量 global YSData; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %判断‘数据标准化’下组合框中的各单选按钮的选中情况,并存储在Index_radiobutton中 Index_radiobutton=get([handles.radiobutton1,handles.radiobutton2,... handles.radiobutton3,handles.radiobutton4,... handles.radiobutton5,handles.radiobutton6,... handles.radiobutton7],'Value'); %进行总和标准化 if(Index_radiobutton{1}==1) SUMData=sum(YSData); [n,m]=size(YSData); BZHData=YSData./SUMData(ones(n,1),:); %进行最大值标准化 elseif(Index_radiobutton{2}==1) MAXData=max(YSData); [n,m]=size(YSData); BZHData=YSData./MAXData(ones(n,1),:); %进行模标准化 elseif(Index_radiobutton{3}==1) MOData=sqrt(sum(YSData.^2)); [n,m]=size(YSData); BZHData=YSData./MOData(ones(n,1),:); %进行中心标准化 elseif(Index_radiobutton{4}==1) MEANData=mean(YSData); [n,m]=size(YSData); BZHData=YSData-MEANData(ones(n,1),:); %进行标准差标准化 elseif(Index_radiobutton{5}==1) BZCData=std(YSData); [n,m]=size(YSData); BZHData=YSData./BZCData(ones(n,1),:); %进行级差标准化 elseif(Index_radiobutton{6}==1) MEANData=mean(YSData); [n,m]=size(YSData); TempData1=YSData-MEANData(ones(n,1),:); Temp=minmax(YSData'); TempData2=(Temp(:,2)-Temp(:,1))'; BZHData=TempData1./TempData2(ones(n,1),:); %进行级差正规化 elseif(Index_radiobutton{7}==1) MINData=min(YSData); [n,m]=size(YSData); TempData1=YSData-MINData(ones(n,1),:); Temp=minmax(YSData'); TempData2=(Temp(:,2)-Temp(:,1))'; BZHData=TempData1./TempData2(ones(n,1),:); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %判断是否用标准化后的数据进行主成分分析 if(get(handles.checkbox1,'Value')==1) [PC,SCORE,latent,tsquare]=princomp(BZHData); else [PC,SCORE,latent,tsquare]=princomp(YSData); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %判断‘参数设置’下组合框中的各单选按钮的选中情况,并对编辑框中输入数据的合理性进行判断 Index_radiobutton=get([handles.radiobutton8,handles.radiobutton9],'Value'); if(Index_radiobutton{1}==1) percent=str2num(get(handles.edit1,'String')); isempty(percent) if(percent<=0|percent>1) msgbox('填写的数据必须在0和1之间','数据错误','error','modal'); return; elseif(isempty(percent)) msgbox('填写的数据不能为空','数据错误','error','modal'); return; end elseif(Index_radiobutton{2}==1) number=round(str2num(get(handles.edit2,'String'))); if(number<1|number>m) msgbox('填写的数据必须是0和变量数之间的整数','数据错误','error','modal'); return; elseif(isempty(number)) msgbox('填写的数据不能为空','数据错误','error','modal'); return; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %判断‘输出数据’下组合框中的各复选按钮的选中情况,并存储在Index_checkbox中 Index_checkbox=get([handles.checkbox2,handles.checkbox3,... handles.checkbox4,handles.checkbox5,... handles.checkbox6,handles.checkbox7,... handles.checkbox8,handles.checkbox9],'Value'); %数据输出%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %输出主成分系数矩阵 if(Index_checkbox{1}==1) coefficient=PC(:,1:number); end %输出主成分得分系数 if(Index_checkbox{2}==1) score_num=SCORE(:,1:number); end %输出载荷矩阵 if(Index_checkbox{3}==1) temp1=latent(1:number)'; temp2=temp1(ones(m,1),:).^0.5; compmat=coefficient.*temp2; end %输出主成分贡献率向量 if(Index_checkbox{4}==1) temp1=100*latent/sum(latent)'; offer=temp1(1:number)'; end %图形输出%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %如果图形输出的四个复选框中只要有一个被选中,那么就建立一个绘图窗体 if(Index_checkbox{5}==1|Index_checkbox{6}==1|Index_checkbox{7}==1|Index_checkbox{8}==1) figure('Name','图形输出','MenuBar','none','NumberTitle','off'); end %在绘图窗体左上角绘制各主成分贡献率直方图 if(Index_checkbox{5}==1) subplot(221); pareto(offer); title('各主成分贡献率直方图'); end %在绘图窗体右上角绘制方差贡献散点图 if(Index_checkbox{6}==1) subplot(222); plot(latent,'r+'); title('方差贡献散点图'); end %在绘图窗体左下角绘制方差贡献山麓图 if(Index_checkbox{7}==1) subplot(223); plot(latent,'g-'); title('方差贡献山麓图'); end %在绘图窗体右下角绘制前两个主成分散点图 if(Index_checkbox{8}==1&str2num(get(handles.edit2,'String'))>=2) subplot(224); plot(score_num(:,1),score_num(:,2),'+'); title('前两个主成分散点图'); 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) %恢复程序开始时各控件的状态 set(handles.radiobutton8,'Value',1); set(handles.radiobutton9,'Value',0); set(handles.edit1,'Enable','on'); set(handles.edit2,'Enable','off','String',''); set(handles.checkbox2,'Value',1); set(handles.checkbox3,'Value',1); set(handles.checkbox4,'Value',1); set(handles.checkbox5,'Value',1); set(handles.checkbox6,'Value',1); set(handles.checkbox7,'Value',1); set(handles.checkbox8,'Value',1); set(handles.checkbox9,'Value',1); set(handles.radiobutton1,'Enable','on','Value',0); set(handles.radiobutton2,'Enable','on','Value',0); set(handles.radiobutton3,'Enable','on','Value',0); set(handles.radiobutton4,'Enable','on','Value',0); set(handles.radiobutton5,'Enable','on','Value',1); set(handles.radiobutton6,'Enable','on','Value',0); set(handles.radiobutton7,'Enable','on','Value',0); set(handles.checkbox1,'Value',1); set(handles.pushbutton2,'Enable','off'); set(handles.edit1,'String','0.8'); % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %关闭程序 close; % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %帮助信息 msgbox('skdjflskdj','Test msgbox','help','modal');