# auto_classify

## PURPOSE

AUTO_CLASSIFY M-file for auto_classify.fig

## SYNOPSIS

function varargout = auto_classify(varargin)

## DESCRIPTION

     AUTO_CLASSIFY M-file for auto_classify.fig
          AUTO_CLASSIFY, by itself, creates a new AUTO_CLASSIFY or raises the existing
          singleton*.

          H = AUTO_CLASSIFY returns the handle to a new AUTO_CLASSIFY or the handle to
          the existing singleton*.

          AUTO_CLASSIFY('CALLBACK',hObject,eventData,handles,...) calls the local
          function named CALLBACK in AUTO_CLASSIFY.M with the given input arguments.

          AUTO_CLASSIFY('Property','Value',...) creates a new AUTO_CLASSIFY or raises the
          existing singleton*.  Starting from the left, property value pairs are
          applied to the GUI before auto_classify_OpeningFunction gets called.  An
          unrecognized property name or invalid value makes property application
          stop.  All inputs are passed to auto_classify_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

## CROSS-REFERENCE INFORMATION

This function calls:

- close VR=CLOSE(VR)
- close VW=CLOSE(VW)
- close VR=CLOSE(VR)
- close VW=CLOSE(VW)
- close VR=CLOSE(VR)
- close VW=CLOSE(VW)

This function is called by:

- classify_spectra CLASSIFY_SPECTRA M-file for classify_spectra.fig

## SUBFUNCTIONS

- function auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)
- function varargout = auto_classify_OutputFcn(hObject, eventdata, handles)
- function clusters = clusterdata(X,k,dmeasure)
- function AutoClassifyButton_Callback(hObject, eventdata, handles)
- function CancelButton_Callback(hObject, eventdata, handles)
- function AcceptButton_Callback(hObject, eventdata, handles)
- function CepstralEdit_Callback(hObject, eventdata, handles)
- function CepstralEdit_CreateFcn(hObject, eventdata, handles)
- function MinClusters_Callback(hObject, eventdata, handles)
- function MinClusters_CreateFcn(hObject, eventdata, handles)
- function MaxClusters_Callback(hObject, eventdata, handles)
- function MaxClusters_CreateFcn(hObject, eventdata, handles)
- function TransformPopupMenu_Callback(hObject, eventdata, handles)
- function TransformPopupMenu_CreateFcn(hObject, eventdata, handles)
- function KClasses_Callback(hObject, eventdata, handles)
- function KClasses_CreateFcn(hObject, eventdata, handles)
- function CepstralPopupMenu_Callback(hObject, eventdata, handles)
- function CepstralPopupMenu_CreateFcn(hObject, eventdata, handles)
- function RangeSpecify_Callback(hObject, eventdata, handles)
- function DistancePopupMenu_Callback(hObject, eventdata, handles)
- function DistancePopupMenu_CreateFcn(hObject, eventdata, handles)
- function DiagnosticCheckbox_Callback(hObject, eventdata, handles)
- function pcaplot(matrix2classify,classification)
- function classifymatrix = madnormalize(classifymatrix, cols2normalize)
- function c=cluster_aylin(p1,p2);
- function [p1,p2]=hneighbors(e);
- function classification = cluster_hierarchical(matrix2classify)
- function HierarchicalRadioButton_Callback(hObject, eventdata, handles)
- function KmeansRadioButton_Callback(hObject, eventdata, handles)
- function SegLenWeight_Callback(hObject, eventdata, handles)
- function SegLenWeight_CreateFcn(hObject, eventdata, handles)

## SOURCE CODE

    0001 function varargout = auto_classify(varargin)
    0002 % AUTO_CLASSIFY M-file for auto_classify.fig
    0003 %      AUTO_CLASSIFY, by itself, creates a new AUTO_CLASSIFY or raises the existing
    0004 %      singleton*.
    0005 %
    0006 %      H = AUTO_CLASSIFY returns the handle to a new AUTO_CLASSIFY or the handle to
    0007 %      the existing singleton*.
    0008 %
    0009 %      AUTO_CLASSIFY('CALLBACK',hObject,eventData,handles,...) calls the local
    0010 %      function named CALLBACK in AUTO_CLASSIFY.M with the given input arguments.
    0011 %
    0012 %      AUTO_CLASSIFY('Property','Value',...) creates a new AUTO_CLASSIFY or raises the
    0013 %      existing singleton*.  Starting from the left, property value pairs are
    0014 %      applied to the GUI before auto_classify_OpeningFunction gets called.  An
    0015 %      unrecognized property name or invalid value makes property application
    0016 %      stop.  All inputs are passed to auto_classify_OpeningFcn via varargin.
    0017 %
    0018 %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
    0019 %      instance to run (singleton)".
    0020 %
    0021 % See also: GUIDE, GUIDATA, GUIHANDLES
    0022
    0023 % Copyright 2002-2003 The MathWorks, Inc.
    0024
    0025 % Edit the above text to modify the response to help auto_classify
    0026
    0027 % Last Modified by GUIDE v2.5 21-Jun-2006 00:34:13
    0028
    0029 % Begin initialization code - DO NOT EDIT
    0030 gui_Singleton = 1;
    0031 gui_State = struct('gui_Name',       mfilename, ...
    0032                    'gui_Singleton',  gui_Singleton, ...
    0033                    'gui_OpeningFcn', @auto_classify_OpeningFcn, ...
    0034                    'gui_OutputFcn',  @auto_classify_OutputFcn, ...
    0035                    'gui_LayoutFcn',  [] , ...
    0036                    'gui_Callback',   []);
    0037 if nargin && ischar(varargin{1})
    0038     gui_State.gui_Callback = str2func(varargin{1});
    0039 end
    0040
    0041 if nargout
    0042     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    0043 else
    0044     gui_mainfcn(gui_State, varargin{:});
    0045 end
    0046 % End initialization code - DO NOT EDIT
    0047
    0048
    0049 % --- Executes just before auto_classify is made visible.
    0050 function auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)
    0051 % This function has no output args, see OutputFcn.
    0052 % hObject    handle to figure
    0053 % eventdata  reserved - to be defined in a future version of MATLAB
    0054 % handles    structure with handles and user data (see GUIDATA)
    0055 % varargin   command line arguments to auto_classify (see VARARGIN)
    0056
    0057 % input parameters
    0058
    0059 if isempty(varargin)
    0060     handles.matrix2classify = [];
    0061     handles.ncepstral = 5;
    0062 else
    0063     handles.matrix2classify = varargin{1};
    0064     handles.ncepstral = varargin{2};
    0065 end
    0066
    0067 handles.distancemeasure = 'sqEuclidean';
    0068
    0069 handles.cluster_method = 'kmeans';
    0070
    0071 set(handles.CepstralPopupMenu,'String',num2str([0:handles.ncepstral]'));
    0072 set(handles.CepstralPopupMenu,'Value',handles.ncepstral); %handles.ncepstral +1 );
    0073 % set(handles.CepstralPopupMenu,'Value',handles.ncepstral-1);
    0074
    0075 % Choose default command line output for auto_classify
    0076 % handles.output = hObject;
    0077
    0078 % Update handles structure
    0079 guidata(hObject, handles);
    0080
    0081 % UIWAIT makes auto_classify wait for user response (see UIRESUME)
    0082 % uiwait(handles.figure1);
    0083
    0084 uiwait(handles.figure1);
    0085
    0086 % --- Outputs from this function are returned to the command line.
    0087 function varargout = auto_classify_OutputFcn(hObject, eventdata, handles)
    0088 % varargout  cell array for returning output args (see VARARGOUT);
    0089 % hObject    handle to figure
    0090 % eventdata  reserved - to be defined in a future version of MATLAB
    0091 % handles    structure with handles and user data (see GUIDATA)
    0092
    0093 % Get default command line output from handles structure
    0094 varargout{1} = handles.output;
    0095
    0096 close;
    0097
    0098 function clusters = clusterdata(X,k,dmeasure)
    0099 % A wrapper function for clustering
    0100
    0101 try
    0102     clusters = kmeans(X,k,'distance',dmeasure,'EmptyAction','singleton','replicates',30);
    0103 catch
    0104     clusters = k;
    0105 end
    0106
    0107 % --- Executes on button press in AutoClassifyButton.
    0108 function AutoClassifyButton_Callback(hObject, eventdata, handles)
    0109 % hObject    handle to AutoClassifyButton (see GCBO)
    0110 % eventdata  reserved - to be defined in a future version of MATLAB
    0111 % handles    structure with handles and user data (see GUIDATA)
    0112
    0113 set(handles.AcceptButton,'Enable','off');
    0114 guidata(gcbo,handles);
    0115
    0116 cncepstral = get(handles.CepstralPopupMenu,'Value');
    0117
    0118 matrix2classify = handles.matrix2classify;
    0119 matrix2classify = madnormalize(matrix2classify,1); % normalize the columns
    0120 matrix2classify = matrix2classify(:,1:cncepstral+1); % include the cepstral coefficients that you want
    0121
    0122 if length(matrix2classify(1,:)) > 3
    0123     matrix2classify = matrix2classify(:,[1,3:cncepstral+1]); % exclude the first cepestral coefficient
    0124 end
    0125
    0126 try
    0127     segweight = str2num(get(handles.SegLenWeight,'String'));
    0128 catch
    0129     segweight = 1;
    0130     set(handles.SegLenWeight,'String','1');
    0131 end
    0132
    0133 % Weight the segment lengths by square root of the weight
    0134 matrix2classify(:,1) = matrix2classify(:,1)*sqrt(segweight);
    0135
    0136 % Other weightings can be added here but the cepestral coefficients have a
    0137 % natural exponential decline which acts a weighting
    0138
    0139 %Handle transformations to segment length
    0140
    0141 contents = get(handles.TransformPopupMenu,'String');
    0142 value = get(handles.TransformPopupMenu,'Value');
    0143
    0144 switch contents{value}
    0145     case 'None'
    0146         matrix2classify = matrix2classify;
    0147     case 'Exclude'
    0148         matrix2classify = matrix2classify(:,2:length(matrix2classify(1,:)));
    0149     case 'Log'
    0150         matrix2classify(:,1) = log(matrix2classify(:,1));
    0151     case 'Exp'
    0152         matrix2classify(:,1) = exp(matrix2classify(:,1));
    0153 end
    0154
    0155 if length(matrix2classify(1,:)) == 0 % matrix to classify has no information
    0156     return
    0157 end
    0158
    0159 rangestate = get(handles.RangeSpecify,'Value');
    0160 diagnosticstate = get(handles.DiagnosticCheckbox,'Value');
    0161
    0162 if strcmp(handles.cluster_method,'hierarchical')
    0163     handles.classification = cluster_hierarchical(matrix2classify);
    0164     nclusters = length(unique(handles.classification)) - 1;
    0165     set(handles.KClasses,'String',nclusters);
    0166
    0167     if diagnosticstate
    0168         pcaplot(matrix2classify,handles.classification);
    0169     end
    0170
    0171 else if strcmp(handles.cluster_method, 'kmeans')
    0172
    0173 if not(rangestate)
    0174
    0175     kclassstr = get(handles.KClasses,'String');
    0176
    0177     if not(isempty(kclassstr))
    0178         try
    0179             nclasses = str2num(kclassstr);
    0180         catch
    0181             return;
    0182         end
    0183
    0184         classification =  clusterdata(matrix2classify,nclasses,handles.distancemeasure);% this can be changed to a different clustering algorithm
    0185
    0186         % clusterdata shows how a clustering algorithm can be hooked in where the number of
    0187         % classes need to be assigned.
    0188
    0189         if length(classification) == 1
    0190             msgbox('Error in clustering. Try changing the number of target clusters or rerunning.');
    0191         else
    0192             if diagnosticstate
    0193                 figure();
    0194                 [silh h] = silhouette(matrix2classify,classification);
    0195                 pcaplot(matrix2classify,classification);
    0196             end
    0197         handles.classification = classification;
    0198         end
    0199     else
    0200         return;
    0201     end
    0202 else
    0203     try
    0204         minclust = str2num(get(handles.MinClusters,'String'));
    0205     catch
    0206         return;
    0207     end
    0208     try
    0209         maxclust = str2num(get(handles.MaxClusters,'String'));
    0210     catch
    0211         return;
    0212     end
    0213
    0214     resultsclust = cell((1+ maxclust)-minclust,1);
    0215
    0216     hw = waitbar(0,'Clustering data set ');
    0217     for i = 1:(1+maxclust)-minclust
    0218         resultsclust{i} = clusterdata(matrix2classify,minclust + (i-1),handles.distancemeasure);
    0219         if length(resultsclust{i}) == 1
    0220             msgbox(['Clustering failed at ' num2str(resultsclust{i}) ' clusters. Try changing the range of clusters or rerunning.']);
    0221             return;
    0222         end
    0223         waitbar(i/(1+maxclust-minclust));
    0224     end
    0225     close(hw);
    0226
    0227     silhmean = zeros((1+ maxclust)-minclust,1);
    0228
    0229     for i = 1:1+(maxclust)-minclust
    0230
    0231         if diagnosticstate
    0232             figure();
    0233             [silh h] = silhouette(matrix2classify,resultsclust{i});
    0234             pcaplot(matrix2classify,resultsclust{i});
    0235         else
    0236             silh = silhouette(matrix2classify,resultsclust{i});
    0237         end
    0238
    0239         silhmean(i) = mean(silh);
    0240     end
    0241
    0242     [x I] = max(silhmean);
    0243     handles.classification = resultsclust{I(1)};
    0244
    0245     set(handles.KClasses,'String',num2str(minclust + (I(1)-1)));
    0246 end
    0247 end
    0248     end
    0249
    0250
    0251 set(handles.AcceptButton,'Enable','on');
    0252
    0253 guidata(gcbo,handles);
    0254
    0255 % --- Executes on button press in CancelButton.
    0256 function CancelButton_Callback(hObject, eventdata, handles)
    0257 % hObject    handle to CancelButton (see GCBO)
    0258 % eventdata  reserved - to be defined in a future version of MATLAB
    0259 % handles    structure with handles and user data (see GUIDATA)
    0260
    0261 handles.output = 0;
    0262 guidata(hObject,handles);
    0263 uiresume(handles.figure1);
    0264
    0265 % --- Executes on button press in AcceptButton.
    0266 function AcceptButton_Callback(hObject, eventdata, handles)
    0267 % hObject    handle to AcceptButton (see GCBO)
    0268 % eventdata  reserved - to be defined in a future version of MATLAB
    0269 % handles    structure with handles and user data (see GUIDATA)
    0270
    0271 handles.output = handles.classification;
    0272 guidata(hObject,handles);
    0273 uiresume(handles.figure1);
    0274
    0275
    0276 function CepstralEdit_Callback(hObject, eventdata, handles)
    0277 % hObject    handle to CepstralEdit (see GCBO)
    0278 % eventdata  reserved - to be defined in a future version of MATLAB
    0279 % handles    structure with handles and user data (see GUIDATA)
    0280
    0281 % Hints: get(hObject,'String') returns contents of CepstralEdit as text
    0282 %        str2double(get(hObject,'String')) returns contents of CepstralEdit as a double
    0283
    0284
    0285 % --- Executes during object creation, after setting all properties.
    0286 function CepstralEdit_CreateFcn(hObject, eventdata, handles)
    0287 % hObject    handle to CepstralEdit (see GCBO)
    0288 % eventdata  reserved - to be defined in a future version of MATLAB
    0289 % handles    empty - handles not created until after all CreateFcns called
    0290
    0291 % Hint: edit controls usually have a white background on Windows.
    0292 %       See ISPC and COMPUTER.
    0293 if ispc
    0294     set(hObject,'BackgroundColor','white');
    0295 else
    0296     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0297 end
    0298
    0299
    0300
    0301 function MinClusters_Callback(hObject, eventdata, handles)
    0302 % hObject    handle to MinClusters (see GCBO)
    0303 % eventdata  reserved - to be defined in a future version of MATLAB
    0304 % handles    structure with handles and user data (see GUIDATA)
    0305
    0306 % Hints: get(hObject,'String') returns contents of MinClusters as text
    0307 %        str2double(get(hObject,'String')) returns contents of MinClusters as a double
    0308
    0309
    0310 % --- Executes during object creation, after setting all properties.
    0311 function MinClusters_CreateFcn(hObject, eventdata, handles)
    0312 % hObject    handle to MinClusters (see GCBO)
    0313 % eventdata  reserved - to be defined in a future version of MATLAB
    0314 % handles    empty - handles not created until after all CreateFcns called
    0315
    0316 % Hint: edit controls usually have a white background on Windows.
    0317 %       See ISPC and COMPUTER.
    0318 if ispc
    0319     set(hObject,'BackgroundColor','white');
    0320 else
    0321     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0322 end
    0323
    0324
    0325
    0326 function MaxClusters_Callback(hObject, eventdata, handles)
    0327 % hObject    handle to MaxClusters (see GCBO)
    0328 % eventdata  reserved - to be defined in a future version of MATLAB
    0329 % handles    structure with handles and user data (see GUIDATA)
    0330
    0331 % Hints: get(hObject,'String') returns contents of MaxClusters as text
    0332 %        str2double(get(hObject,'String')) returns contents of MaxClusters as a double
    0333
    0334
    0335 % --- Executes during object creation, after setting all properties.
    0336 function MaxClusters_CreateFcn(hObject, eventdata, handles)
    0337 % hObject    handle to MaxClusters (see GCBO)
    0338 % eventdata  reserved - to be defined in a future version of MATLAB
    0339 % handles    empty - handles not created until after all CreateFcns called
    0340
    0341 % Hint: edit controls usually have a white background on Windows.
    0342 %       See ISPC and COMPUTER.
    0343 if ispc
    0344     set(hObject,'BackgroundColor','white');
    0345 else
    0346     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0347 end
    0348
    0349
    0350 % --- Executes on selection change in TransformPopupMenu.
    0351 function TransformPopupMenu_Callback(hObject, eventdata, handles)
    0352 % hObject    handle to TransformPopupMenu (see GCBO)
    0353 % eventdata  reserved - to be defined in a future version of MATLAB
    0354 % handles    structure with handles and user data (see GUIDATA)
    0355
    0356 % Hints: contents = get(hObject,'String') returns TransformPopupMenu contents as cell array
    0357 %        contents{get(hObject,'Value')} returns selected item from TransformPopupMenu
    0358
    0359 % value = get(hObject,'Value');
    0360 %
    0361 % if value == 2
    0362 %     handles.matrix2classify(:,1) = log(handles.matrix2classify(:,1));
    0363 % else
    0364 %     handles.matrix2classify(:,1) = exp(handles.matrix2classify(:,1));
    0365 % end
    0366
    0367 % --- Executes during object creation, after setting all properties.
    0368 function TransformPopupMenu_CreateFcn(hObject, eventdata, handles)
    0369 % hObject    handle to TransformPopupMenu (see GCBO)
    0370 % eventdata  reserved - to be defined in a future version of MATLAB
    0371 % handles    empty - handles not created until after all CreateFcns called
    0372
    0373 % Hint: popupmenu controls usually have a white background on Windows.
    0374 %       See ISPC and COMPUTER.
    0375 if ispc
    0376     set(hObject,'BackgroundColor','white');
    0377 else
    0378     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0379 end
    0380
    0381 function KClasses_Callback(hObject, eventdata, handles)
    0382 % hObject    handle to KClasses (see GCBO)
    0383 % eventdata  reserved - to be defined in a future version of MATLAB
    0384 % handles    structure with handles and user data (see GUIDATA)
    0385
    0386 % Hints: get(hObject,'String') returns contents of KClasses as text
    0387 %        str2double(get(hObject,'String')) returns contents of KClasses as a double
    0388
    0389
    0390 % --- Executes during object creation, after setting all properties.
    0391 function KClasses_CreateFcn(hObject, eventdata, handles)
    0392 % hObject    handle to KClasses (see GCBO)
    0393 % eventdata  reserved - to be defined in a future version of MATLAB
    0394 % handles    empty - handles not created until after all CreateFcns called
    0395
    0396 % Hint: edit controls usually have a white background on Windows.
    0397 %       See ISPC and COMPUTER.
    0398 if ispc
    0399     set(hObject,'BackgroundColor','white');
    0400 else
    0401     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0402 end
    0403
    0404
    0405 % --- Executes on selection change in CepstralPopupMenu.
    0406 function CepstralPopupMenu_Callback(hObject, eventdata, handles)
    0407 % hObject    handle to CepstralPopupMenu (see GCBO)
    0408 % eventdata  reserved - to be defined in a future version of MATLAB
    0409 % handles    structure with handles and user data (see GUIDATA)
    0410
    0411 % Hints: contents = get(hObject,'String') returns CepstralPopupMenu contents as cell array
    0412 %        contents{get(hObject,'Value')} returns selected item from CepstralPopupMenu
    0413
    0414
    0415 % --- Executes during object creation, after setting all properties.
    0416 function CepstralPopupMenu_CreateFcn(hObject, eventdata, handles)
    0417 % hObject    handle to CepstralPopupMenu (see GCBO)
    0418 % eventdata  reserved - to be defined in a future version of MATLAB
    0419 % handles    empty - handles not created until after all CreateFcns called
    0420
    0421 % Hint: popupmenu controls usually have a white background on Windows.
    0422 %       See ISPC and COMPUTER.
    0423 if ispc
    0424     set(hObject,'BackgroundColor','white');
    0425 else
    0426     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0427 end
    0428
    0429
    0430
    0431 % --- Executes on button press in RangeSpecify.
    0432 function RangeSpecify_Callback(hObject, eventdata, handles)
    0433 % hObject    handle to RangeSpecify (see GCBO)
    0434 % eventdata  reserved - to be defined in a future version of MATLAB
    0435 % handles    structure with handles and user data (see GUIDATA)
    0436
    0437 % Hint: get(hObject,'Value') returns toggle state of RangeSpecify
    0438
    0439 state = get(hObject,'Value');
    0440
    0441 if state
    0442     set(handles.KClasses,'Enable','off');
    0443     set(handles.text3,'Visible','on');
    0444     set(handles.MinClusters,'Visible','on');
    0445     set(handles.text4,'Visible','on');
    0446     set(handles.MaxClusters,'Visible','on');
    0447 else
    0448     set(handles.KClasses,'Enable','on');
    0449     set(handles.text3,'Visible','off');
    0450     set(handles.MinClusters,'Visible','off');
    0451     set(handles.text4,'Visible','off');
    0452     set(handles.MaxClusters,'Visible','off');
    0453 end
    0454
    0455
    0456 % --- Executes on selection change in DistancePopupMenu.
    0457 function DistancePopupMenu_Callback(hObject, eventdata, handles)
    0458 % hObject    handle to DistancePopupMenu (see GCBO)
    0459 % eventdata  reserved - to be defined in a future version of MATLAB
    0460 % handles    structure with handles and user data (see GUIDATA)
    0461
    0462 % Hints: contents = get(hObject,'String') returns DistancePopupMenu contents as cell array
    0463 %        contents{get(hObject,'Value')} returns selected item from DistancePopupMenu
    0464
    0465 contents = get(hObject,'String');
    0466
    0467 dstring = contents{get(hObject,'Value')};
    0468
    0469 switch dstring
    0470     case 'Squared Euclidean'
    0471         handles.distancemeasure = 'sqEuclidean'
    0472     case 'City Block (L1)'
    0473         handles.distancemeasure = 'cityblock'
    0474     case 'Cosine'
    0475         handles.distancemeasure = 'cosine'
    0476     case 'Correlation'
    0477         handles.distancemeasure = 'correlation'
    0478 end
    0479
    0480 guidata(gcbo,handles);
    0481
    0482 % --- Executes during object creation, after setting all properties.
    0483 function DistancePopupMenu_CreateFcn(hObject, eventdata, handles)
    0484 % hObject    handle to DistancePopupMenu (see GCBO)
    0485 % eventdata  reserved - to be defined in a future version of MATLAB
    0486 % handles    empty - handles not created until after all CreateFcns called
    0487
    0488 % Hint: popupmenu controls usually have a white background on Windows.
    0489 %       See ISPC and COMPUTER.
    0490 if ispc
    0491     set(hObject,'BackgroundColor','white');
    0492 else
    0493     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0494 end
    0495
    0496 handles.distancemeasure = 'sqEuclidean';
    0497
    0498 guidata(gcbo,handles);
    0499
    0500 % --- Executes on button press in DiagnosticCheckbox.
    0501 function DiagnosticCheckbox_Callback(hObject, eventdata, handles)
    0502 % hObject    handle to DiagnosticCheckbox (see GCBO)
    0503 % eventdata  reserved - to be defined in a future version of MATLAB
    0504 % handles    structure with handles and user data (see GUIDATA)
    0505
    0506 % Hint: get(hObject,'Value') returns toggle state of DiagnosticCheckbox
    0507
    0508 function pcaplot(matrix2classify,classification)
    0509 % Plots the first two principal components uses different shapes selected
    0510 % randomly and different colors selected randomly to show classes.
    0511
    0512 nclusters = length(unique(classification));
    0513
    0514 if not(isempty(find(classification == 0)))
    0515     nclusters = nclusters -1;
    0516 end
    0517
    0518 colors = rand(nclusters,3);
    0519 mark = mod(1:nclusters,13)+1;
    0520
    0521 [coef, score] = princomp(matrix2classify);
    0522
    0523 figure();
    0524
    0525 for i = 1:length(classification)
    0526     h = line(score(i,1),score(i,2));
    0527     markers = set(h,'Marker');
    0528     if classification(i) == 0 % For segments which are not classified
    0529         set(h,'Marker',markers{4});
    0530         set(h,'Color',[0 0 0]);
    0531     else
    0532         set(h,'Marker',markers{mark(classification(i))});
    0533         set(h,'Color',colors(classification(i),:));
    0534     end
    0535 end
    0536
    0537 xlabel('PCA 1');
    0538 ylabel('PCA 2');
    0539
    0540 function classifymatrix = madnormalize(classifymatrix, cols2normalize)
    0541   for i = cols2normalize
    0542     classifymatrix(:,i) = (classifymatrix(:,i) - median(classifymatrix(:,i))) / mad(classifymatrix(:,i));
    0543   end
    0544
    0545
    0546 % This code is modified from http://phys.columbia.edu/~aylin/clustering
    0547
    0548 function c=cluster_aylin(p1,p2);
    0549 %enter the p1,p2 found from hneighbors.m and get the struct array c.
    0550 %c(i).c will contain the indices of the objects that is in cluster i.
    0551 %c(1).c will be the cluster that has the maximum number of objects.
    0552 nn=size(p1,2);
    0553 p=std(p1);g=p(p2);p=p';
    0554 z=zeros(nn,1);zz=zeros(nn,1);
    0555 n=zeros(1,nn);
    0556 for i=1:nn
    0557     n(i)=max(max(p1(:,p2(:,i))));
    0558 end
    0559 gg=[1:nn; max(g); n]';n=find(n<1);
    0560 [q1,q1]=sort(gg(n,2)');
    0561 g=gg(n(q1),1);
    0562 t=cputime;
    0563 j=1;m=1
    0564     for i=1:length(n)
    0565         ii=g(i);b=p2(:,ii);a=b;aa=a(find(gg(a,2)<1));
    0566         if length([0; unique(z(aa))])<=2;a1=1;a2=0;
    0567             while a1~=a2;a1=length(aa);
    0568                 a=unique(p2(:,a));a=a(find(gg(a,2)<1));
    0569                 a=unique(a(find(gg(a,2)<=mean(gg(aa,2))+std(gg(aa,2)))));
    0570                 a=a(find(ismember(a,aa)==0));
    0571                 if ~isempty(a);aa=[aa;a];
    0572                     jj=aa(find(z(aa)));
    0573                     if ~isempty(jj);;
    0574                          u=unique(z(jj));
    0575                          if length(u)==1;
    0576                             zz(aa(find(~z(aa))))=m;z(aa)=u;m=m+1;
    0577                          end
    0578                          break;
    0579                     end
    0580                 end;a2=length(aa);
    0581             end;a=aa;
    0582             jj=a(find(z(a)));
    0583             if isempty(jj);
    0584                 z(a)=j;j=j+1;
    0585                 zz(a)=m;zz(b)=m-.1;zz(ii)=m-.2;m=m+1;
    0586             end
    0587         end
    0588     end
    0589     u=unique(z)
    0590     u=u(find(u));v=length(u);vv=floor(1/v);if vv;vv=' is';else vv='s      are';end
    0591     fprintf([int2str(v) ' cluster' vv ' found in ' num2str(cputime-t) 'sec\n\n'])
    0592     q0=[];for i=1:v;qq=find(z==u(i));q0=[q0 length(qq)];end;
    0593     [q1,q2]=sort(q0);%q2=q2(find(100*q1/nn>1));v=length(q2);
    0594     c=[];for i=1:v;qq=find(z==u(q2(i)));[j,j]=sort(zz(qq));c(v-i+1).c=qq(j);end;
    0595     if isempty(c);fprintf('\tNo cluster was found. \n \tscale the data (step size must be 1)\n');end
    0596
    0597
    0598     function [p1,p2]=hneighbors(e);
    0599     % this function finds the neighbors of each object in 'e' within a unit hypercube
    0600     % and returns the sorted object distances to q.q1 and their identities to q.q2 , 'e' is a
    0601     % matrix where i th row and j th column represents the j th component of the i th object.
    0602     s='find(';i='abs(e(:,%d)-e(i,%d))<1&';
    0603     % assuming that the data is given scaled and the characteristic step size is 1, variable s
    0604     % keeps a script to find the objects that lie within a unit hypercube around the i th object.
    0605     for j=1:size(e,2);
    0606         s=[s sprintf(i,j,j)];
    0607     end;s([end end+1])=');';
    0608      % runs the script s for each of the objects and stores the sorted distances
    0609      % from the i th object in q(i).q1 and their indentities in q(i).q2
    0610     nn=size(e,1);m=ceil(nn^(1/4));p1=ones(m,nn);p2=kron(ones(m,1),1:nn);
    0611     for i=1:nn;
    0612         j=eval(s);
    0613         [q,qq]=sort(sqrt(sum((e(j,:)-kron(ones(length(j),1),e(i,:)))'.^2)));q=q(find(q<1));mm=length(q);
    0614         qq=j(qq(1:mm))';mn=min([m mm]);
    0615         p1(1:mn,i)=q(1:mn)';
    0616         p2(1:mn,i)=qq(1:mn)';
    0617     end
    0618
    0619
    0620 function classification = cluster_hierarchical(matrix2classify)
    0621     [p1,p2] = hneighbors(matrix2classify);
    0622     c = cluster_aylin(p1,p2);
    0623
    0624     nclusters = size(c,2);
    0625
    0626     total_classified = 0
    0627
    0628     nsegments = size(matrix2classify,1);
    0629
    0630     classification = zeros(nsegments,1);
    0631
    0632     for i = 1:nclusters
    0633         for j = 1:length(c(i).c)
    0634             classification(c(i).c(j)) = i;
    0635         end
    0636     end
    0637 ;
    0638
    0639
    0640 % --- Executes on button press in HierarchicalRadioButton.
    0641 function HierarchicalRadioButton_Callback(hObject, eventdata, handles)
    0642 % hObject    handle to HierarchicalRadioButton (see GCBO)
    0643 % eventdata  reserved - to be defined in a future version of MATLAB
    0644 % handles    structure with handles and user data (see GUIDATA)
    0645
    0646 % Hint: get(hObject,'Value') returns toggle state of HierarchicalRadioButton
    0647
    0648 selected = get(hObject,'Value');
    0649
    0650 if selected
    0651     handles.cluster_method = 'hierarchical';
    0652 end
    0653
    0654 guidata(gcbo,handles);
    0655
    0656
    0657 % --- Executes on button press in KmeansRadioButton.
    0658 function KmeansRadioButton_Callback(hObject, eventdata, handles)
    0659 % hObject    handle to KmeansRadioButton (see GCBO)
    0660 % eventdata  reserved - to be defined in a future version of MATLAB
    0661 % handles    structure with handles and user data (see GUIDATA)
    0662
    0663 % Hint: get(hObject,'Value') returns toggle state of KmeansRadioButton
    0664
    0665 selected = get(hObject,'Value');
    0666
    0667 if selected
    0668     handles.cluster_method = 'kmeans';
    0669 end
    0670
    0671 guidata(gcbo,handles);
    0672
    0673
    0674
    0675
    0676 function SegLenWeight_Callback(hObject, eventdata, handles)
    0677 % hObject    handle to SegLenWeight (see GCBO)
    0678 % eventdata  reserved - to be defined in a future version of MATLAB
    0679 % handles    structure with handles and user data (see GUIDATA)
    0680
    0681 % Hints: get(hObject,'String') returns contents of SegLenWeight as text
    0682 %        str2double(get(hObject,'String')) returns contents of SegLenWeight as a double
    0683
    0684
    0685 % --- Executes during object creation, after setting all properties.
    0686 function SegLenWeight_CreateFcn(hObject, eventdata, handles)
    0687 % hObject    handle to SegLenWeight (see GCBO)
    0688 % eventdata  reserved - to be defined in a future version of MATLAB
    0689 % handles    empty - handles not created until after all CreateFcns called
    0690
    0691 % Hint: edit controls usually have a white background on Windows.
    0692 %       See ISPC and COMPUTER.
    0693 if ispc
    0694     set(hObject,'BackgroundColor','white');
    0695 else
    0696     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    0697 end
    0698
    0699