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

📄 pcagui.m

📁 PLS_Toolbox是用于故障检测与诊断方面的matlab工具箱
💻 M
📖 第 1 页 / 共 3 页
字号:
function pcagui(action,fighand)
%PCAGUI Principal Components Analysis with graphical user interface.
%  PCAGUI performs principal components analysis using a
%  graphical user interface. Data, variable and sample scales,
%  and variable and sample lables can be loaded from the workspace
%  into the tool by using the PCA_File menu. Scaling options can
%  be set using the PCA_Scale menu. Models can be calculated by
%  by pressing the calc button. Eigenvalues, scores, loadings, biplots
%  and raw data can be viewed by clicking on the appropriate buttons.
%  Models can be saved to the work space using the PCA_File menu,
%  Models can be reloaded and used with new data. Models are saved
%  as structured arrays with the following fields:
%
%     xname: name of the original workspace input variable
%      name: type of model, always 'PCA'
%      date: model creation date stamp
%      time: model creation time stamp
%    scores: PCA scores
%     loads: PCA loadings
%       ssq: sum of squares captured information
%     means: means of the original data
%      stds: variances of the original data
%     scale: scaling used, i.e. none, mncn or auto
%       res: Q residuals
%    reslim: 95% Q limit
%       tsq: T^2 values
%    tsqlim: 95% T^2 limit
%      irow: samples included in model
%      icol: variables included in model
%      drow: samples deleted from model
%      dcol: variables deleted from model
%      sscl: scale for plotting scores against
%      vscl: scale for plotting loadings against
%      slbl: sample labels
%     slbln: sample labels original workspace name
%      vlbl: variable labels
%     vlbln: variable labels original workspace name
%     samps: number of samples in original data
%
%I/O: pcagui
%
%See also: MODLGUI, MODLRDER, PCA, PCAPRO, XPLDST

%Copyright Eigenvector Research, Inc. 1996-98
%nbg 4,6,7,12/97,7/98,10/98,12/98
%nbg 11/00 changed table header and gui size

if nargin<1
  bgc0   = [0 0 0];
  bgc1   = [1 0 1]*0.6;
  bgc2   = [1 1 1]*0.85;
  bgc3   = [1 1 1];
  load pcaprefg
  p      = get(0,'ScreenSize'); 
  ww     = pcaprefg.pcauser.widthheight(1); %380;
  wh     = pcaprefg.pcauser.widthheight(2); %323;
  a = figure('Color',bgc0,'Resize','on', ...
    'Name','Principal Components Analysis', ...
    'HandleVisibility','off', ...
    'NumberTitle','Off','Position',[6 p(4)-wh-40 ww wh]);
  as     = int2str(a);
  
% Menus
  b      = zeros(4,1);
  bb     = zeros(17,1);
% I/O Menu
  b(1,1) = uimenu(a,'Label','&PCA_File');
  bb(1,1)= uimenu(b(1,1),'Label','&Load Data', ...
    'CallBack',['pcagui(''loaddata'',',as,');']);
  bb(2,1)= uimenu(b(1,1),'Label','&Load Model', ...
    'CallBack',['pcagui(''loadmodl'',',as,');']);
  b(3,1) = uimenu(b(1,1),'Label','&Load Scale','Enable','off');
  bb(3,1)= uimenu(b(3,1),'Label','&Sample','Enable','off', ...
    'CallBack',['pcagui(''loadsscl'',',as,');']);
  bb(4,1)= uimenu(b(3,1),'Label','&Variable','Enable','off', ...
    'CallBack',['pcagui(''loadvscl'',',as,');']);
  b(2,1) = uimenu(b(1,1),'Label','&Load Labels','Enable','off');
  bb(5,1)= uimenu(b(2,1),'Label','&Sample','Enable','off', ...
    'CallBack',['pcagui(''loadslbl'',',as,');']);
  bb(6,1)= uimenu(b(2,1),'Label','&Variable','Enable','off', ...
    'CallBack',['pcagui(''loadvlbl'',',as,');']);
  bb(7,1)= uimenu(b(1,1),'Label','&Save Data','Enable','off',  ...
    'CallBack',['pcagui(''savemat'',',as,');'],'Separator','on');
  bb(15,1)= uimenu(b(1,1),'Label','&Save Test','Enable','off',  ...
    'CallBack',['pcagui(''savetst'',',as,');']);
  bb(8,1)= uimenu(b(1,1),'Label','&Save Model','Enable','off',  ...
    'CallBack',['pcagui(''savemodl'',',as,');']);
  bb(17,1)= uimenu(b(1,1),'Label','&Print Info','Enable','off',  ...
    'CallBack',['pcagui(''printssqtable'',',as,');']);
  bb(16,1)= uimenu(b(1,1),'Label','&Preferences','Enable','on',  ...
    'CallBack',['prefpls(',as,');']);
  bb(9,1)= uimenu(b(1,1),'Label','&Clear Data','Enable','off',  ...
    'CallBack',['pcagui(''cleardata'',',as,');'],'Separator','on');
  bb(10,1)= uimenu(b(1,1),'Label','&Clear Model','Enable','off',  ...
    'CallBack',['pcagui(''clearmodl'',',as,');']);
  bb(11,1)= uimenu(b(1,1),'Label','&Exit PCA', ...
    'CallBack',['pcagui(''exitpca'',',as,');'],'Separator','on');    
% Scaling Menu
  b(4,1) = uimenu(a,'Label','&PCA_Scale');
  bb(12,1)= uimenu(b(4,1),'Label','&no scaling','Enable','off', ...
    'CallBack',['pcagui(''actnoscale'',',as,');']);
  bb(13,1)= uimenu(b(4,1),'Label','&mean center','Enable','off', ...
    'CallBack',['pcagui(''actmncn'',',as,');']);
  bb(14,1)= uimenu(b(4,1),'Label','&autoscaling','Enable','off', ...
    'CallBack',['pcagui(''actauto'',',as,');'],'Checked','on');
% Internal StatusBar
  d      = zeros(4,1);
  e      = zeros(6,1);
  d(1,1) = uicontrol('Parent',a,'Style','frame', ...
    'Units','normalized','Position', ...
    [0.0263158 0.736842 0.947368 0.250774]);
% Variance Captured List/text
  d(2,1) = uicontrol('Parent',a,'Style','frame', ...
    'Units','normalized','Position', ...
    [0.0263158 0.0309598 0.778947 0.696594]);
  s = str2mat(...
    '         Percent Variance Captured by PCA Model', ...
    ' ', ...
    '  Principal      Eigenvalue        % Variance    % Variance', ...
    'Component     of Cov(X)          This  PC       Cumulative ');
  e(1,1) = uicontrol('Parent',a, ...
    'Units','normalized','Position', ...
    [0.0342105 0.482972 0.763158 0.170279], ...
    'HorizontalAlignment','left', ...
    'String',s,'Style','text', ...
    'FontName',pcaprefg.pcauser.tableheader.FontName, ...
    'FontUnits',pcaprefg.pcauser.tableheader.FontUnits, ...
    'FontSize',pcaprefg.pcauser.tableheader.FontSize, ...
    'FontWeight',pcaprefg.pcauser.tableheader.FontWeight, ...
    'FontAngle',pcaprefg.pcauser.tableheader.FontAngle, ...
    'UserData','%3.0f       %4.2e   %6.2f    %6.2f');
  e(2,1) = uicontrol('Parent',a, ...
    'Units','normalized','Position', ...
    [0.0342105 0.662539 0.460526 0.0557276], ...
    'String','Number of PCs Selected:', ...
    'HorizontalAlignment','left','FontWeight','bold', ...
    'Style','text','FontName','geneva','Fontsize',10);
  e(3,1) = uicontrol('Parent',a, ...
    'Units','normalized','Position', ...
    [0.502632 0.659443 0.131579 0.0619195],'Fontsize',10, ...
    'String',' ','Style','edit','FontWeight','bold', ...
    'Callback',['pcagui(''actssq2'',',as,');']);
  e(4,1) = uicontrol('Parent',a,'BackgroundColor',bgc3, ...
    'Units','normalized','Position', ...
    [0.0342105 0.0402477 0.763158 0.433437],'Style','listbox', ...
    'FontName',pcaprefg.pcauser.tablebody.FontName, ...
    'FontUnits',pcaprefg.pcauser.tablebody.FontUnits, ...
    'FontSize',pcaprefg.pcauser.tablebody.FontSize, ...
    'FontWeight',pcaprefg.pcauser.tablebody.FontWeight, ...
    'FontAngle',pcaprefg.pcauser.tablebody.FontAngle, ...
    'Value',1,'String',[], ...
    'Callback',['pcagui(''actssq'',',as,');']);
  %Text for internal status bar
  s = str2mat('Data: none loaded');
  e(5,1) =  uicontrol('Parent',a,'BackgroundColor',bgc3, ...
    'Units','normalized','Position', ...
    [0.0342105 0.74613 0.461842 0.232198], ...
    'HorizontalAlignment','left', ...
    'Style','text','String',s, ...
    'FontName',pcaprefg.pcauser.statwindow.FontName, ...
    'FontUnits',pcaprefg.pcauser.statwindow.FontUnits, ...
    'FontSize',pcaprefg.pcauser.statwindow.FontSize, ...
    'FontWeight',pcaprefg.pcauser.statwindow.FontWeight, ...
    'FontAngle',pcaprefg.pcauser.statwindow.FontAngle);
  s = ['Model: none loaded'];
  e(6,1) =  uicontrol('Parent',a,'BackgroundColor',bgc3, ...
    'Units','normalized','Position', ...
    [0.503947 0.74613 0.461842 0.232198], ...
    'HorizontalAlignment','left', ...
    'Style','text','String',s, ...
    'FontName',pcaprefg.pcauser.statwindow.FontName, ...
    'FontUnits',pcaprefg.pcauser.statwindow.FontUnits, ...
    'FontSize',pcaprefg.pcauser.statwindow.FontSize, ...
    'FontWeight',pcaprefg.pcauser.statwindow.FontWeight, ...
    'FontAngle',pcaprefg.pcauser.statwindow.FontAngle);
  if strcmp(lower(computer),'mac2')
    s = str2mat(...
      '         Percent Variance Captured by PCA Model', ...
      ' ', ...
      'Principal      Eigenvalue       % Variance     % Variance', ...
      'Component     of Cov(X)          This  PC        Cumulative ');
    set(e(1,1),'String',s, ...
      'UserData',' %3.0f       %4.2e     %6.2f      %6.2f')
  end
% Buttons
  d(3,1) = uicontrol('Parent',a,'Style','frame', ...
    'Units','normalized','Position', ...
  [0.810526 0.544892 0.160526 0.182663]);
  d(4,1) = uicontrol('Parent',a,'Style','frame', ...
    'Units','normalized','Position', ...
  [0.810526 0.0309598 0.160526 0.50774]);
  c = uicontrol('Parent',a, ...
    'BackgroundColor',bgc1,'ForegroundColor',bgc3, ...
    'Style','text', ...
    'Units','normalized','Position', ...
    [0.818421 0.464396 0.144737 0.0619195],'String','plots', ...
    'FontName',pcaprefg.pcauser.buttons.FontName, ...
    'FontUnits',pcaprefg.pcauser.buttons.FontUnits, ...
    'FontSize',pcaprefg.pcauser.buttons.FontSize, ...
    'FontWeight',pcaprefg.pcauser.buttons.FontWeight, ...
    'FontAngle',pcaprefg.pcauser.buttons.FontAngle);
  f      = zeros(7,1);
  f(1,1) = uicontrol('Parent',a, ...
    'CallBack',['pcagui(''calculate'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.640867 0.144737 0.0773994],'String','calc', ...
    'TooltipString','calculate PCA model');
  f(2,1) = uicontrol('Parent',a, ...
    'CallBack',['pcagui(''apply'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.55418 0.144737 0.0773994],'String','apply', ...
    'TooltipString','apply PCA model');
  f(3,1) = uicontrol('Parent',a, ...
    'CallBack',['pcaplot1(''plotscree'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.386997 0.144737 0.0773994],'String','eigen', ...
    'TooltipString','plot eigenvalues');
  f(4,1) = uicontrol('Parent',a, ...
    'CallBack',['pcaplots(''plotscores'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.30031 0.144737 0.0773994],'String','scores', ...
    'TooltipString','plot scores and sample statistics');
  f(5,1) = uicontrol('Parent',a, ...
    'CallBack',['pcaplots(''plotloads'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.213622 0.144737 0.0773994],'String','loads', ...
    'TooltipString','plot loads and variable statistics');
  f(6,1) = uicontrol('Parent',a, ...
    'CallBack',['pcaplot1(''biplot'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.126935 0.144737 0.0773994],'String','biplot', ...
    'TooltipString','plot scores and loadings biplots');
  f(7,1) = uicontrol('Parent',a, ...
    'CallBack',['pcaplot1(''rawplot'',',as,');'], ...
    'Units','normalized','Position', ...
    [0.818421 0.0402477 0.144737 0.0773994],'String','data', ...
    'TooltipString','plot raw data');

  %Set common properties
  set(d(1:length(d),1),'BackgroundColor',bgc1)
  set(e(1:length(e),1),'BackgroundColor',bgc3)
  set(f(1:length(f),1),'BackgroundColor',bgc2,'Enable','off', ...
    'FontName',pcaprefg.pcauser.buttons.FontName, ...
    'FontUnits',pcaprefg.pcauser.buttons.FontUnits, ...
    'FontSize',pcaprefg.pcauser.buttons.FontSize, ...
    'FontWeight',pcaprefg.pcauser.buttons.FontWeight, ...
    'FontAngle',pcaprefg.pcauser.buttons.FontAngle);

  set(a,'CloseRequestfcn',['pcagui(''exitpca'',',as,');'])
  %Assign Handles
  set(a,'UserData',d)         %Frame Handles
  set(d(1,1),'UserData',b)    %UIMenu Handles
  set(d(2,1),'UserData',e)    %Text Handles
  set(d(3,1),'UserData',f)    %Button Handles
  set(b(3,1),'UserData',bb)
  stat.data  = 'none';
  stat.modl  = 'none';
  modl       = [];
  test       = [];
  set(b(1,1),'UserData',stat)
  set(f(2,1),'UserData',modl)
  set(f(5,1),'UserData',test)
  pcagui('cleardata',a);
  pcagui('clearmodl',a);
else
  %Get handles
  d    = get(fighand,'UserData'); %Frame Handles
  a    = get(d(1,1),'Parent');
  as   = int2str(a);
  b    = get(d(1,1),'UserData');  %UIMenu Handles
  e    = get(d(2,1),'UserData');  %Text Handles
  f    = get(d(3,1),'UserData');  %Button Handles
  x    = get(f(1,1),'UserData');  %Data Matrix
  bb   = get(b(3,1),'UserData');  %UIsubmenu handles
  stat = get(b(1,1),'UserData');  %Data/Model Status
  modl = get(f(2,1),'UserData');  %Model
  test = get(f(5,1),'UserData');  %Test Data
  
  switch lower(action)
  case 'loaddata'
    lddlgpls(f(1,1),d(4,1),'double')
    x             = get(f(1,1),'UserData');
    if isempty(x)
      pcagui('cleardata',a);
    elseif class(x)~='double'
      erdlgpls('variable not double array','Error on Load Data!')
      pcagui('cleardata',a);
    elseif size(x,1)==1|size(x,2)==1
      erdlgpls('variable must be a matrix','Error on Load Data!')
      pcagui('cleardata',a);
    else
      stat.data   = 'new';
      if strcmp(stat.modl,'none')
        modl      = get(f(2,1),'UserData');
        modl.irow = 1:size(x,1);
        modl.icol = 1:size(x,2);
      else
        stat.modl = 'loaded';
      end

⌨️ 快捷键说明

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