📄 pcagui.m
字号:
%Scaling
switch modl.scale
case 'mean' %mean center
[x,modl.means] = mncn(x);
modl.stds = std(x);
case 'auto' %autoscale
[x,modl.means,modl.stds] = auto(x);
otherwise %no scaling
modl.means = mean(x);
modl.stds = std(x);
end
%SVD
if n<m
cx = (x'*x)/(m-1);
[u,s,v] = svd(cx);
pcs = (1:n)';
else
cx = (x*x')/(m-1);
[u,s,v] = svd(cx);
v = x'*v;
for i=1:m
v(:,i) = v(:,i)/norm(v(:,i));
end
pcs = (1:m)';
end
s = diag(s);
cap = s*100/(sum(s));
pc = 1; %default model
modl.name = 'PCA';
modl.date = date; %date model was created
modl.time = clock; %time model was created
modl.scores = x*v(:,1:pc); %PCA scores
modl.samps = m; %Number of samples in (data)
modl.loads = v(:,1:pc); %PCA loadings
modl.ssq = [pcs s cap cumsum(cap)]; %Variance information
modl.xname = get(d(4,1),'UserData');
res = (x - modl.scores*modl.loads')';
res = sum(res.^2)';
%Q Residuals and Limit
modl.res = res; %Sample residuals Q
modl.reslim = reslim(pc,s,95); %95% conf limit for Q
%T^2 and Limit
cap = 1./sqrt(s(1:pc,1));
cap = modl.scores*diag(cap);
if pc>1
tsqs = sum((cap.^2)')';
else
tsqs = (cap.^2);
end
modl.tsq = tsqs; %Sample Hotelling T^2
modl.tsqlim = tsqlim(m,pc,95); %95% confidence limit for T^2
format= get(e(1,1),'UserData');
s = [];
for jj=1:min([size(modl.ssq,1); 25]) %max PCs listed = 25
s = [s;sprintf(format,modl.ssq(jj,:))];
end
set(f(3,1),'UserData',v)
set(f(4,1),'UserData',pc);
set(e(3,1),'String',int2str(pc))
set(e(4,1),'String',s,'Value',pc)
stat.data = 'cal';
stat.modl = 'calold';
else
stat.modl = 'none';
end
else
stat.data = 'none';
end
set(bb(15,1),'Enable','off')
case 'apply'
delfigs(as)
if strcmp(stat.modl,'calnew')
if ~isempty(modl.drow)
jk = [];
for jj=1:length(modl.drow)
jk = [jk,find(modl.irow==modl.drow(jj))];
end
modl.irow = delsamps(modl.irow',jk)';
end
x = x(modl.irow,modl.icol);
end
n = size(x,2);
m = size(modl.loads,1);
if size(x,2)~=m
erdlgpls('Error- num vars in x ~= num rows of loads', ...
'Error on Apply');
else
switch modl.scale
case 'mean'
x = scale(x,modl.means);
case 'auto'
x = scale(x,modl.means,modl.stds);
end
m = get(f(4,1),'UserData');
if ~strcmp(stat.modl,'loaded')
v = get(f(3,1),'UserData');
if ~isempty(v)
modl.loads = v(:,1:m);
end
clear v
modl.reslim = reslim(m,modl.ssq(:,2),95);
modl.tsqlim = tsqlim(length(modl.irow),m,95);
modl.scores = x*modl.loads;
modl.res = (x - modl.scores*modl.loads')';
modl.res = sum((modl.res).^2)';
modl.tsq = 1./sqrt(modl.ssq(1:m,2));
modl.tsq = modl.scores*diag(modl.tsq);
if m>1
modl.tsq = sum(((modl.tsq).^2)')';
else
modl.tsq = (modl.tsq).^2;
end
set(bb(15,1),'Enable','off')
else
test.scores = x*modl.loads;
test.res = (x - (test.scores)*(modl.loads)')';
test.res = sum((test.res).^2)';
test.tsq = 1./sqrt(modl.ssq(1:m,2));
test.tsq = test.scores*diag(test.tsq);
test.xname = get(d(4,1),'UserData');
if m>1
test.tsq = sum(((test.tsq).^2)')';
else
test.tsq = (test.tsq).^2;
end
set(bb(15,1),'Enable','on')
end
if strcmp(stat.data,'new')
stat.data = 'test';
stat.modl = 'loaded';
end
if strcmp(stat.data,'cal')
stat.modl = 'calold';
end
end
case 'actssq'
if strcmp(stat.modl,'loaded')
n = get(f(4,1),'UserData');
set(e(3,1),'String',int2str(n))
set(e(4,1),'Value',n)
elseif ~strcmp(stat.data,'none')
n = get(e(4,1),'Value');
set(e(3,1),'String',int2str(n))
set(f(4,1),'UserData',n);
stat.modl = 'calnew';
end
case 'actssq2'
if strcmp(stat.modl,'loaded')
n = get(f(4,1),'UserData');
set(e(3,1),'String',int2str(n))
set(e(4,1),'Value',n)
elseif ~strcmp(stat.data,'none')&~strcmp(stat.modl,'none')
n = str2num(get(e(3,1),'String'));
n = round(n);
if (n<1)|(n>max(modl.ssq(:,1)))
n = 1;
end
set(e(4,1),'Value',n);
set(e(3,1),'String',int2str(n))
set(f(4,1),'UserData',n);
stat.modl = 'calnew';
end
case 'printssqtable'
ssqtable(modl,size(modl.loads,2));
end
%button status
if ~strcmp(lower(action),'exitpca')
set(b(1,1),'UserData',stat)
set(f(2,1),'UserData',modl)
set(f(5,1),'UserData',test)
if strcmp(stat.data,'none')&strcmp(stat.modl,'none')
%no data, no model
set(f(1:7,1),'Enable','off')
elseif ~strcmp(stat.modl,'none')&strcmp(stat.data,'none')
%no data, a model
set(f([1:2 4:7],1),'Enable','off')
set(f(3,1),'Enable','on')
elseif ~strcmp(stat.data,'none')&strcmp(stat.modl,'none')
%new data, no model
set(f([1 7],1),'Enable','on')
set(f(2:6,1),'Enable','off')
elseif ~strcmp(stat.data,'none')&(~strcmp(stat.modl,'none'))
%data and model
if strcmp(stat.modl,'calold')
set(f(1:2,1),'Enable','off')
set(f(3:7,1),'Enable','on')
elseif strcmp(stat.modl,'calnew')
set(f([1 4:6],1),'Enable','off')
set(f([2:3 7],1),'Enable','on')
elseif strcmp(stat.modl,'loaded')
if strcmp(stat.data,'test')
set(f(1:2,1),'Enable','off')
set(f(3:7,1),'Enable','on')
else
set(f([1 3:6],1),'Enable','off')
set(f([2 7],1),'Enable','on')
end
end
end
if get(f(4,1),'UserData')<2
set(f(6,1),'Enable','off') %biplot button
h = findobj('Name','Biplot','Tag',as); close(h)
end
%scaling menu
if strcmp(stat.modl,'loaded')|strcmp(stat.data,'none')
set(bb(12:14,1),'Enable','off')
else
set(bb(12:14,1),'Enable','on')
end
%status strings and file menu
if strcmp(stat.modl,'none')
set(f(3:4,1),'UserData',[])
set(e(3,1),'String',' ')
set(e(4,1),'String',' ','Value',1)
if strcmp(stat.data,'none')
s = ['Model: none loaded'];
else
s = ['Model: not calculated'];
end
set(e(6,1),'String',s)
set(bb([8:10 17],1),'Enable','off');
else
set(bb([8:10 17],1),'Enable','on');
switch stat.modl
case 'loaded'
if strcmp(stat.data,'none')
s = ['Model: loaded'];
elseif strcmp(stat.data,'new')
s = ['Model: loaded but not applied'];
elseif strcmp(stat.data,'test')
s = ['Model: loaded and applied'];
end
case 'calnew'
s = ['Model: not applied'];
case 'calold'
s = ['Model: calibrated on loaded data'];
end
modl = get(f(2,1),'UserData');
pc = size(modl.loads,2);
if strcmp(modl.scale,'auto')
sc = 'autoscaled';
elseif strcmp(modl.scale,'mean')
sc = 'mean centered';
else
sc = 'not scaled';
end
s1 = ['PC(s): ',int2str(pc)];
s2 = ['Scaling: ',sc];
s3 = ['Data: ',int2str(length(modl.irow)),' sams x '];
s3 = [s3,[int2str(length(modl.icol)),' vars']];
set(e(6,1),'String',str2mat(s,s1,s3,s2))
end
if strcmp(stat.data,'none')
set(f(1,1),'UserData',[])
set(e(5,1),'String','Data: none loaded')
set(bb([3:7 9],1),'Enable','off');
else
set(b(2:3,1),'Enable','on')
set(bb([3:6 9],1),'Enable','on')
if modl.drow|modl.dcol
set(bb(7,1),'Enable','on');
end
if ~strcmp(stat.modl,'loaded')
set(bb([4 6],1),'Enable','on');
else
set(bb([4 6],1),'Enable','off');
end
if strcmp(stat.data,'new')
s = ['Data: loaded but not analyzed'];
s4 = ['Samp Lbls: ',modl.slbln];
elseif strcmp(stat.data,'cal')
s = ['Data: modeled (calibration set)'];
s4 = ['Samp Lbls: ',modl.slbln];
elseif strcmp(stat.data,'test')
s = ['Data: modeled (test set)'];
s4 = ['Samp Lbls: ',test.slbln];
end
[m,n] = size(get(f(1,1),'UserData'));
s3 = ['Var: ',get(d(4,1),'UserData')];
s2 = ['Size: ',int2str(m),' rows x ',int2str(n),' cols'];
s5 = ['Var Lbls: ',modl.vlbln];
set(e(5,1),'String',str2mat(s3,s,s2,s4,s5))
end
end
end
function [] = delfigs(as)
h = findobj('Name','Eigenvalues','Tag',as); close(h)
h = findobj('Name','Plot Scores','Tag',as); close(h)
h = findobj('Name','Plot Loads','Tag',as); close(h)
h = findobj('Name','Biplot','Tag',as); close(h)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -