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

% Last Modified by GUIDE v2.5 25-Mar-2002 12:19:15

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pca_win_OpeningFcn, ...
                   'gui_OutputFcn',  @pca_win_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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_win is made visible.
function pca_win_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_win (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes pca_win wait for user response (see UIRESUME)
% uiwait(handles.figure1);

z=varargin{1};
handles.prefs = varargin{2};
handles.markers = varargin{3};
guidata(hObject, handles);

col_ord=[0 0 0 ; 0 0 1 ; 0 1 0 ; 1 0 0 ; 1 0.9 0 ; 1 0 1 ; 0 1 1 ; 0.5 0.5 0.5 ; 0.2 0.5 0 ; 0.4 0 0.1]; 

set(handles.output,'name','PCA Plot');
set(handles.output,'Renderer','zbuffer');
set(handles.output,'DoubleBuffer','on');
axes(handles.axes1); 


if handles.prefs.pca.man_axes==1
    axis manual
    cla;
else
    reset(handles.axes1);
    cla;
end    
set(handles.axes1,'colororder',col_ord);
hold on
xlabel('Component 1');
ylabel('Component 2');

if handles.prefs.pca.comps==2
    for n=1:10
        class=handles.prefs.pca.class;
        for k=1:length(class)
            if k<handles.prefs.pca.mincallno || k>handles.prefs.pca.maxcallno
                class(k)=0;
            end
        end
        find_a=find(class==n);
        if ~isempty(find_a) && handles.prefs.pca.use_calls(n)==1
            
            if handles.prefs.pca.datapoints==1
                plot(z(find_a,1),z(find_a,2),'.','color',col_ord(n,:))
            end 
            if handles.prefs.pca.datanumbers==1
                text(z(find_a,1),z(find_a,2),int2str(find_a),'color',col_ord(n,:))
            end
            CallVal(n)=n;
        else
            %plot(mean(z(:,1)),mean(z(:,2)),'.w')    
        end
        
    end
%     for n=1:8
%         if handles.markers.use(n)==1;
%             plot( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),'o','color',col_ord(n+2,:))
%             plot( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),'+','color',col_ord(n+2,:)*0.5)
%         end
%     end
elseif handles.prefs.pca.comps==3
    for n=1:10
        class=handles.prefs.pca.class;
        for k=1:length(class)
            if k<handles.prefs.pca.mincallno || k>handles.prefs.pca.maxcallno
                class(k)=0;
            end
        end
        find_a=find(class==n);
        if ~isempty(find_a) && handles.prefs.pca.use_calls(n)==1
            if handles.prefs.pca.datapoints==1
                plot3(z(find_a,1),z(find_a,2),z(find_a,3),'.','color',col_ord(n,:))
            end
            if handles.prefs.pca.datanumbers==1
                text(z(find_a,1),z(find_a,2),z(find_a,3),int2str(find_a),'color',col_ord(n,:))
            end
            CallVal(n)=n;
        else
            %plot3(mean(z(:,1)),mean(z(:,2)),mean(z(:,3)),'.w')    
        end
    end
%     for n=1:8
%         if handles.markers.use(n)==1;
%             plot3( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),z(size(z,1)-(8-n),3),'o','color',col_ord(n+2,:))
%             plot3( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),z(size(z,1)-(8-n),3),'+','color',col_ord(n+2,:)*0.5)
%         end
%     end
end


if handles.prefs.pca.comps==2
elseif handles.prefs.pca.comps==3
    zlabel('Component 3');
    set(handles.axes1,'view',[75 75]);
end

%CallVal = [handles.prefs.pca.use_calls(1), handles.prefs.pca.use_calls(2), handles.prefs.pca.use_calls(3), handles.prefs.pca.use_calls(4), handles.prefs.pca.use_calls(5), handles.prefs.pca.use_calls(6), handles.prefs.pca.use_calls(7), handles.prefs.pca.use_calls(8), handles.prefs.pca.use_calls(9), handles.prefs.pca.use_calls(10)];
CallTyp = [{'Other'}, {'Null'}, {'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}, {'G'}, {'H'}];

call_choose = find(CallVal);
% bob = CallTyp(call_choose)
% CallTyp(1)

legend(CallTyp(call_choose),1,'Location','Best')

if handles.prefs.pca.comps==3
    for n=1:8
        if handles.markers.use(n)==1;
            plot3( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),z(size(z,1)-(8-n),3),'d','markerfacecolor',col_ord(n+2,:),'markeredgecolor',col_ord(n+2,:)*0.8,'markersize',8)
            %plot3( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),z(size(z,1)-(8-n),3),'o','color',col_ord(n+2,:))
            %plot3( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),z(size(z,1)-(8-n),3),'.','color',col_ord(n+2,:))
        end
    end
elseif handles.prefs.pca.comps==2
    for n=1:8
        if handles.markers.use(n)==1;
            %plot( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),'o','color',col_ord(n+2,:))
            plot( z(size(z,1)-(8-n),1),z(size(z,1)-(8-n),2),'d','markerfacecolor',col_ord(n+2,:),'markeredgecolor',col_ord(n+2,:)*0.8,'markersize',8)
        end
    end
end
hold off

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