⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pca_gui.m

📁 PCA应用程序
💻 M
字号:
 function varargout = PCA_GUI(varargin)
% PCA_GUI M-file for PCA_GUI.fig
%      PCA_GUI, by itself, creates a new PCA_GUI or raises the existing
%      singleton*.
%
%      H = PCA_GUI returns the handle to a new PCA_GUI or the handle to
%      the existing singleton*.
%
%      PCA_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PCA_GUI.M with the given input arguments.
%
%      PCA_GUI('Property','Value',...) creates a new PCA_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before PCA_GUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to PCA_GUI_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 PCA_GUI

% Last Modified by GUIDE v2.5 16-Mar-2006 15:03:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @PCA_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @PCA_GUI_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 PCA_GUI is made visible.
function PCA_GUI_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 PCA_GUI (see VARARGIN)

% Choose default command line output for PCA_GUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes PCA_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.pushbutton2,'Enable','off');
set(handles.pushbutton3,'Enable','off');
set(handles.pushbutton4,'Enable','off');
set(handles.radiobutton1,'Value',1);
handles.popup = '标准差标准化';
guidata(hObject,handles)


% --- Outputs from this function are returned to the command line.
function varargout = PCA_GUI_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 popupmenu1.
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)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
val = get(hObject,'Value');
str = get(hObject,'String');
handles.popup = str{val};
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
set(handles.pushbutton3,'Enable','off');
set(handles.pushbutton4,'Enable','off');


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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
set(handles.pushbutton3,'Enable','off');
set(handles.pushbutton4,'Enable','off');


% --- 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


function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
set(handles.pushbutton3,'Enable','off');
set(handles.pushbutton4,'Enable','off');


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 radiobutton8.
function radiobutton1_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.radiobutton1,'Value',1);
    set(handles.radiobutton2,'Value',0);
    set(handles.edit1,'Enable','on');
    set(handles.edit2,'Enable','off','String','');


% --- Executes on button press in radiobutton9.
function radiobutton2_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.radiobutton1,'Value',0);
    set(handles.radiobutton2,'Value',1);
    set(handles.edit1,'Enable','off','String','');
    set(handles.edit2,'Enable','on');


% --- 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)
[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 error');
    return;
end
%读取样本维数和样本个数
mn=fscanf(fid,'%f,%f',2);
%获取样本矩阵(temp是一个元胞数组)
temp=textscan(fid,'%f','returnOnError',0);
X=vec2mat(temp{1},mn(2));
handles.XData=X;
guidata(hObject,handles);
%只有正确打开数据文件后,才能激活‘开始分析’按钮
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)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%判断‘标准化方法’下拉菜单的选择情况,调用标准化函数
index=handles.popup;
X=handles.XData;
[X1]=XStd(X,index);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%主元分析
m=length(X1(1,:));
n=length(X1(:,1));
[pc,score,latent,tsquare] = princomp(X1);

handles.pc=pc;
handles.score=score;
handles.latent=latent;
handles.tsquare=tsquare;
guidata(hObject,handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%判断计算主元个数的方法,如果选择按贡献率,则计算主元个数k
Index_radiobutton=get([handles.radiobutton1,handles.radiobutton2],'Value');
if(Index_radiobutton{1}==1)
    kp=round(str2num(get(handles.edit1,'String')));
    if(kp<1|kp>m)
        msgbox('该值必须是0和样本维数之间的整数','数据错误','error','modal');
        return;
    elseif(isempty(kp))
        msgbox('该值不能为空','数据错误','error','modal');
        return;
    end
elseif(Index_radiobutton{2}==1)
    percent=str2num(get(handles.edit2,'String'))
    if(percent<=0|percent>1)
        msgbox('该值必须在0和1之间','数据错误','error','modal');
        return;
    elseif(isempty(percent))
        msgbox('该值不能为空','数据错误','error','modal');
        return;
    end
    latent_sum=0;
    block=0;
    for i=1:m
        latent_sum=latent_sum+latent(i);
        if latent_sum/sum(latent) >= percent & block==0;
            kp=i;
            block=1;
        end
    end
end
handles.kp=kp;
guidata(hObject,handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%调用pca_thre函数计算阈值限
alpha=str2num(get(handles.edit3,'String'));
if(alpha<=0|alpha>1)
    msgbox('该值必须在0和1之间','数据错误','error','modal');
    return;
elseif(isempty(alpha))
    msgbox('该值不能为空','数据错误','error','modal');
    return;
end
[ts_ctrl,spe_ctrl]=PCAThrd(X1,latent,alpha,kp);
handles.ts_ctrl=ts_ctrl;
handles.spe_ctrl=spe_ctrl;
guidata(hObject,handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%只有分析之后,才能激活‘结果保存’和‘图形输出’按钮
set(handles.pushbutton3,'Enable','on');
set(handles.pushbutton4,'Enable','on');


% --- 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)

[filename,pathname,FilterIndex]=uiputfile({'*.mat';'*.*'},'保存数据文件');
if(FilterIndex==0)
    return;
end
str_filename=[pathname,filename];
save(str_filename);


% --- 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)
XData=handles.XData;
latent=handles.latent;
pc=handles.pc;
score=handles.score;
kp=handles.kp;
%主元贡献率向量
offer=100*latent/sum(latent)';

%图形输出%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1);
subplot(221);
bar(offer(1:kp),'m');
title('各主元贡献率直方图');

subplot(222);
plot(latent,'r+');
title('方差贡献散点图');

subplot(223);
plot(latent,'g-');
title('方差贡献山麓图');

if(kp>=2)
    subplot(224);
    plot(score(:,1),score(:,2),'*');
    title('一、二主元平面分布图');
end

figure(2);



% --- 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)
set(handles.pushbutton2,'Enable','off');
set(handles.pushbutton3,'Enable','off');
set(handles.pushbutton4,'Enable','off');
set(handles.radiobutton1,'Value',1);
set(handles.radiobutton2,'Value',0);
set(handles.edit1,'String',3);
set(handles.edit2,'String',0.8);
set(handles.edit3,'String',0.9);
set(handles.popupmenu1,'Value',1);
handles.popup = '标准差标准化';
guidata(hObject,handles)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -