auto_classify.html 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/REC-html40/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Description of auto_classify</title>
  6. <meta name="keywords" content="auto_classify">
  7. <meta name="description" content="AUTO_CLASSIFY M-file for auto_classify.fig">
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  9. <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  10. <meta name="robots" content="index, follow">
  11. <link type="text/css" rel="stylesheet" href="../../m2html.css">
  12. <script type="text/javascript">
  13. if (top.frames.length == 0) { top.location = "../../index.html"; };
  14. </script>
  15. </head>
  16. <body>
  17. <a name="_top"></a>
  18. <!-- ../menu.html chronux_2_10 --><!-- menu.html wave_browser -->
  19. <h1>auto_classify
  20. </h1>
  21. <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  22. <div class="box"><strong>AUTO_CLASSIFY M-file for auto_classify.fig</strong></div>
  23. <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  24. <div class="box"><strong>function varargout = auto_classify(varargin) </strong></div>
  25. <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  26. <div class="fragment"><pre class="comment"> AUTO_CLASSIFY M-file for auto_classify.fig
  27. AUTO_CLASSIFY, by itself, creates a new AUTO_CLASSIFY or raises the existing
  28. singleton*.
  29. H = AUTO_CLASSIFY returns the handle to a new AUTO_CLASSIFY or the handle to
  30. the existing singleton*.
  31. AUTO_CLASSIFY('CALLBACK',hObject,eventData,handles,...) calls the local
  32. function named CALLBACK in AUTO_CLASSIFY.M with the given input arguments.
  33. AUTO_CLASSIFY('Property','Value',...) creates a new AUTO_CLASSIFY or raises the
  34. existing singleton*. Starting from the left, property value pairs are
  35. applied to the GUI before auto_classify_OpeningFunction gets called. An
  36. unrecognized property name or invalid value makes property application
  37. stop. All inputs are passed to auto_classify_OpeningFcn via varargin.
  38. *See GUI Options on GUIDE's Tools menu. Choose &quot;GUI allows only one
  39. instance to run (singleton)&quot;.
  40. See also: GUIDE, GUIDATA, GUIHANDLES</pre></div>
  41. <!-- crossreference -->
  42. <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  43. This function calls:
  44. <ul style="list-style-image:url(../../matlabicon.gif)">
  45. <li><a href="../../chronux_2_10/fly_track/videoIO/videoIO_2006a/@videoReader/close.html" class="code" title="function vr = close(vr)">close</a> VR=CLOSE(VR)</li><li><a href="../../chronux_2_10/fly_track/videoIO/videoIO_2006a/@videoWriter/close.html" class="code" title="function vw = close(vw)">close</a> VW=CLOSE(VW)</li><li><a href="../../chronux_2_10/fly_track/videoIO/videoIO_2006b/@videoReader/close.html" class="code" title="function vr = close(vr)">close</a> VR=CLOSE(VR)</li><li><a href="../../chronux_2_10/fly_track/videoIO/videoIO_2006b/@videoWriter/close.html" class="code" title="function vw = close(vw)">close</a> VW=CLOSE(VW)</li><li><a href="../../chronux_2_10/fly_track/videoIO/videoIO_2007a/@videoReader/close.html" class="code" title="function vr = close(vr)">close</a> VR=CLOSE(VR)</li><li><a href="../../chronux_2_10/fly_track/videoIO/videoIO_2007a/@videoWriter/close.html" class="code" title="function vw = close(vw)">close</a> VW=CLOSE(VW)</li></ul>
  46. This function is called by:
  47. <ul style="list-style-image:url(../../matlabicon.gif)">
  48. <li><a href="classify_spectra.html" class="code" title="function varargout = classify_spectra(varargin)">classify_spectra</a> CLASSIFY_SPECTRA M-file for classify_spectra.fig</li></ul>
  49. <!-- crossreference -->
  50. <h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  51. <ul style="list-style-image:url(../../matlabicon.gif)">
  52. <li><a href="#_sub1" class="code">function auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)</a></li><li><a href="#_sub2" class="code">function varargout = auto_classify_OutputFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub3" class="code">function clusters = clusterdata(X,k,dmeasure)</a></li><li><a href="#_sub4" class="code">function AutoClassifyButton_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub5" class="code">function CancelButton_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub6" class="code">function AcceptButton_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub7" class="code">function CepstralEdit_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub8" class="code">function CepstralEdit_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub9" class="code">function MinClusters_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub10" class="code">function MinClusters_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub11" class="code">function MaxClusters_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub12" class="code">function MaxClusters_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub13" class="code">function TransformPopupMenu_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub14" class="code">function TransformPopupMenu_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub15" class="code">function KClasses_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub16" class="code">function KClasses_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub17" class="code">function CepstralPopupMenu_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub18" class="code">function CepstralPopupMenu_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub19" class="code">function RangeSpecify_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub20" class="code">function DistancePopupMenu_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub21" class="code">function DistancePopupMenu_CreateFcn(hObject, eventdata, handles)</a></li><li><a href="#_sub22" class="code">function DiagnosticCheckbox_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub23" class="code">function pcaplot(matrix2classify,classification)</a></li><li><a href="#_sub24" class="code">function classifymatrix = madnormalize(classifymatrix, cols2normalize)</a></li><li><a href="#_sub25" class="code">function c=cluster_aylin(p1,p2);</a></li><li><a href="#_sub26" class="code">function [p1,p2]=hneighbors(e);</a></li><li><a href="#_sub27" class="code">function classification = cluster_hierarchical(matrix2classify)</a></li><li><a href="#_sub28" class="code">function HierarchicalRadioButton_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub29" class="code">function KmeansRadioButton_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub30" class="code">function SegLenWeight_Callback(hObject, eventdata, handles)</a></li><li><a href="#_sub31" class="code">function SegLenWeight_CreateFcn(hObject, eventdata, handles)</a></li></ul>
  53. <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  54. <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function varargout = auto_classify(varargin)</a>
  55. 0002 <span class="comment">% AUTO_CLASSIFY M-file for auto_classify.fig</span>
  56. 0003 <span class="comment">% AUTO_CLASSIFY, by itself, creates a new AUTO_CLASSIFY or raises the existing</span>
  57. 0004 <span class="comment">% singleton*.</span>
  58. 0005 <span class="comment">%</span>
  59. 0006 <span class="comment">% H = AUTO_CLASSIFY returns the handle to a new AUTO_CLASSIFY or the handle to</span>
  60. 0007 <span class="comment">% the existing singleton*.</span>
  61. 0008 <span class="comment">%</span>
  62. 0009 <span class="comment">% AUTO_CLASSIFY('CALLBACK',hObject,eventData,handles,...) calls the local</span>
  63. 0010 <span class="comment">% function named CALLBACK in AUTO_CLASSIFY.M with the given input arguments.</span>
  64. 0011 <span class="comment">%</span>
  65. 0012 <span class="comment">% AUTO_CLASSIFY('Property','Value',...) creates a new AUTO_CLASSIFY or raises the</span>
  66. 0013 <span class="comment">% existing singleton*. Starting from the left, property value pairs are</span>
  67. 0014 <span class="comment">% applied to the GUI before auto_classify_OpeningFunction gets called. An</span>
  68. 0015 <span class="comment">% unrecognized property name or invalid value makes property application</span>
  69. 0016 <span class="comment">% stop. All inputs are passed to auto_classify_OpeningFcn via varargin.</span>
  70. 0017 <span class="comment">%</span>
  71. 0018 <span class="comment">% *See GUI Options on GUIDE's Tools menu. Choose &quot;GUI allows only one</span>
  72. 0019 <span class="comment">% instance to run (singleton)&quot;.</span>
  73. 0020 <span class="comment">%</span>
  74. 0021 <span class="comment">% See also: GUIDE, GUIDATA, GUIHANDLES</span>
  75. 0022
  76. 0023 <span class="comment">% Copyright 2002-2003 The MathWorks, Inc.</span>
  77. 0024
  78. 0025 <span class="comment">% Edit the above text to modify the response to help auto_classify</span>
  79. 0026
  80. 0027 <span class="comment">% Last Modified by GUIDE v2.5 21-Jun-2006 00:34:13</span>
  81. 0028
  82. 0029 <span class="comment">% Begin initialization code - DO NOT EDIT</span>
  83. 0030 gui_Singleton = 1;
  84. 0031 gui_State = struct(<span class="string">'gui_Name'</span>, mfilename, <span class="keyword">...</span>
  85. 0032 <span class="string">'gui_Singleton'</span>, gui_Singleton, <span class="keyword">...</span>
  86. 0033 <span class="string">'gui_OpeningFcn'</span>, @<a href="#_sub1" class="code" title="subfunction auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)">auto_classify_OpeningFcn</a>, <span class="keyword">...</span>
  87. 0034 <span class="string">'gui_OutputFcn'</span>, @<a href="#_sub2" class="code" title="subfunction varargout = auto_classify_OutputFcn(hObject, eventdata, handles)">auto_classify_OutputFcn</a>, <span class="keyword">...</span>
  88. 0035 <span class="string">'gui_LayoutFcn'</span>, [] , <span class="keyword">...</span>
  89. 0036 <span class="string">'gui_Callback'</span>, []);
  90. 0037 <span class="keyword">if</span> nargin &amp;&amp; ischar(varargin{1})
  91. 0038 gui_State.gui_Callback = str2func(varargin{1});
  92. 0039 <span class="keyword">end</span>
  93. 0040
  94. 0041 <span class="keyword">if</span> nargout
  95. 0042 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  96. 0043 <span class="keyword">else</span>
  97. 0044 gui_mainfcn(gui_State, varargin{:});
  98. 0045 <span class="keyword">end</span>
  99. 0046 <span class="comment">% End initialization code - DO NOT EDIT</span>
  100. 0047
  101. 0048
  102. 0049 <span class="comment">% --- Executes just before auto_classify is made visible.</span>
  103. 0050 <a name="_sub1" href="#_subfunctions" class="code">function auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)</a>
  104. 0051 <span class="comment">% This function has no output args, see OutputFcn.</span>
  105. 0052 <span class="comment">% hObject handle to figure</span>
  106. 0053 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  107. 0054 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  108. 0055 <span class="comment">% varargin command line arguments to auto_classify (see VARARGIN)</span>
  109. 0056
  110. 0057 <span class="comment">% input parameters</span>
  111. 0058
  112. 0059 <span class="keyword">if</span> isempty(varargin)
  113. 0060 handles.matrix2classify = [];
  114. 0061 handles.ncepstral = 5;
  115. 0062 <span class="keyword">else</span>
  116. 0063 handles.matrix2classify = varargin{1};
  117. 0064 handles.ncepstral = varargin{2};
  118. 0065 <span class="keyword">end</span>
  119. 0066
  120. 0067 handles.distancemeasure = <span class="string">'sqEuclidean'</span>;
  121. 0068
  122. 0069 handles.cluster_method = <span class="string">'kmeans'</span>;
  123. 0070
  124. 0071 set(handles.CepstralPopupMenu,<span class="string">'String'</span>,num2str([0:handles.ncepstral]'));
  125. 0072 set(handles.CepstralPopupMenu,<span class="string">'Value'</span>,handles.ncepstral); <span class="comment">%handles.ncepstral +1 );</span>
  126. 0073 <span class="comment">% set(handles.CepstralPopupMenu,'Value',handles.ncepstral-1);</span>
  127. 0074
  128. 0075 <span class="comment">% Choose default command line output for auto_classify</span>
  129. 0076 <span class="comment">% handles.output = hObject;</span>
  130. 0077
  131. 0078 <span class="comment">% Update handles structure</span>
  132. 0079 guidata(hObject, handles);
  133. 0080
  134. 0081 <span class="comment">% UIWAIT makes auto_classify wait for user response (see UIRESUME)</span>
  135. 0082 <span class="comment">% uiwait(handles.figure1);</span>
  136. 0083
  137. 0084 uiwait(handles.figure1);
  138. 0085
  139. 0086 <span class="comment">% --- Outputs from this function are returned to the command line.</span>
  140. 0087 <a name="_sub2" href="#_subfunctions" class="code">function varargout = auto_classify_OutputFcn(hObject, eventdata, handles) </a>
  141. 0088 <span class="comment">% varargout cell array for returning output args (see VARARGOUT);</span>
  142. 0089 <span class="comment">% hObject handle to figure</span>
  143. 0090 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  144. 0091 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  145. 0092
  146. 0093 <span class="comment">% Get default command line output from handles structure</span>
  147. 0094 varargout{1} = handles.output;
  148. 0095
  149. 0096 <a href="../../chronux_2_10/fly_track/videoIO/videoIO_2006a/@videoReader/close.html" class="code" title="function vr = close(vr)">close</a>;
  150. 0097
  151. 0098 <a name="_sub3" href="#_subfunctions" class="code">function clusters = clusterdata(X,k,dmeasure)</a>
  152. 0099 <span class="comment">% A wrapper function for clustering</span>
  153. 0100
  154. 0101 <span class="keyword">try</span>
  155. 0102 clusters = kmeans(X,k,<span class="string">'distance'</span>,dmeasure,<span class="string">'EmptyAction'</span>,<span class="string">'singleton'</span>,<span class="string">'replicates'</span>,30);
  156. 0103 <span class="keyword">catch</span>
  157. 0104 clusters = k;
  158. 0105 <span class="keyword">end</span>
  159. 0106
  160. 0107 <span class="comment">% --- Executes on button press in AutoClassifyButton.</span>
  161. 0108 <a name="_sub4" href="#_subfunctions" class="code">function AutoClassifyButton_Callback(hObject, eventdata, handles)</a>
  162. 0109 <span class="comment">% hObject handle to AutoClassifyButton (see GCBO)</span>
  163. 0110 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  164. 0111 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  165. 0112
  166. 0113 set(handles.AcceptButton,<span class="string">'Enable'</span>,<span class="string">'off'</span>);
  167. 0114 guidata(gcbo,handles);
  168. 0115
  169. 0116 cncepstral = get(handles.CepstralPopupMenu,<span class="string">'Value'</span>);
  170. 0117
  171. 0118 matrix2classify = handles.matrix2classify;
  172. 0119 matrix2classify = <a href="#_sub24" class="code" title="subfunction classifymatrix = madnormalize(classifymatrix, cols2normalize)">madnormalize</a>(matrix2classify,1); <span class="comment">% normalize the columns</span>
  173. 0120 matrix2classify = matrix2classify(:,1:cncepstral+1); <span class="comment">% include the cepstral coefficients that you want</span>
  174. 0121
  175. 0122 <span class="keyword">if</span> length(matrix2classify(1,:)) &gt; 3
  176. 0123 matrix2classify = matrix2classify(:,[1,3:cncepstral+1]); <span class="comment">% exclude the first cepestral coefficient</span>
  177. 0124 <span class="keyword">end</span>
  178. 0125
  179. 0126 <span class="keyword">try</span>
  180. 0127 segweight = str2num(get(handles.SegLenWeight,<span class="string">'String'</span>));
  181. 0128 <span class="keyword">catch</span>
  182. 0129 segweight = 1;
  183. 0130 set(handles.SegLenWeight,<span class="string">'String'</span>,<span class="string">'1'</span>);
  184. 0131 <span class="keyword">end</span>
  185. 0132
  186. 0133 <span class="comment">% Weight the segment lengths by square root of the weight</span>
  187. 0134 matrix2classify(:,1) = matrix2classify(:,1)*sqrt(segweight);
  188. 0135
  189. 0136 <span class="comment">% Other weightings can be added here but the cepestral coefficients have a</span>
  190. 0137 <span class="comment">% natural exponential decline which acts a weighting</span>
  191. 0138
  192. 0139 <span class="comment">%Handle transformations to segment length</span>
  193. 0140
  194. 0141 contents = get(handles.TransformPopupMenu,<span class="string">'String'</span>);
  195. 0142 value = get(handles.TransformPopupMenu,<span class="string">'Value'</span>);
  196. 0143
  197. 0144 <span class="keyword">switch</span> contents{value}
  198. 0145 <span class="keyword">case</span> <span class="string">'None'</span>
  199. 0146 matrix2classify = matrix2classify;
  200. 0147 <span class="keyword">case</span> <span class="string">'Exclude'</span>
  201. 0148 matrix2classify = matrix2classify(:,2:length(matrix2classify(1,:)));
  202. 0149 <span class="keyword">case</span> <span class="string">'Log'</span>
  203. 0150 matrix2classify(:,1) = log(matrix2classify(:,1));
  204. 0151 <span class="keyword">case</span> <span class="string">'Exp'</span>
  205. 0152 matrix2classify(:,1) = exp(matrix2classify(:,1));
  206. 0153 <span class="keyword">end</span>
  207. 0154
  208. 0155 <span class="keyword">if</span> length(matrix2classify(1,:)) == 0 <span class="comment">% matrix to classify has no information</span>
  209. 0156 <span class="keyword">return</span>
  210. 0157 <span class="keyword">end</span>
  211. 0158
  212. 0159 rangestate = get(handles.RangeSpecify,<span class="string">'Value'</span>);
  213. 0160 diagnosticstate = get(handles.DiagnosticCheckbox,<span class="string">'Value'</span>);
  214. 0161
  215. 0162 <span class="keyword">if</span> strcmp(handles.cluster_method,<span class="string">'hierarchical'</span>)
  216. 0163 handles.classification = <a href="#_sub27" class="code" title="subfunction classification = cluster_hierarchical(matrix2classify)">cluster_hierarchical</a>(matrix2classify);
  217. 0164 nclusters = length(unique(handles.classification)) - 1;
  218. 0165 set(handles.KClasses,<span class="string">'String'</span>,nclusters);
  219. 0166
  220. 0167 <span class="keyword">if</span> diagnosticstate
  221. 0168 <a href="#_sub23" class="code" title="subfunction pcaplot(matrix2classify,classification)">pcaplot</a>(matrix2classify,handles.classification);
  222. 0169 <span class="keyword">end</span>
  223. 0170
  224. 0171 <span class="keyword">else</span> <span class="keyword">if</span> strcmp(handles.cluster_method, <span class="string">'kmeans'</span>)
  225. 0172
  226. 0173 <span class="keyword">if</span> not(rangestate)
  227. 0174
  228. 0175 kclassstr = get(handles.KClasses,<span class="string">'String'</span>);
  229. 0176
  230. 0177 <span class="keyword">if</span> not(isempty(kclassstr))
  231. 0178 <span class="keyword">try</span>
  232. 0179 nclasses = str2num(kclassstr);
  233. 0180 <span class="keyword">catch</span>
  234. 0181 <span class="keyword">return</span>;
  235. 0182 <span class="keyword">end</span>
  236. 0183
  237. 0184 classification = <a href="#_sub3" class="code" title="subfunction clusters = clusterdata(X,k,dmeasure)">clusterdata</a>(matrix2classify,nclasses,handles.distancemeasure);<span class="comment">% this can be changed to a different clustering algorithm</span>
  238. 0185
  239. 0186 <span class="comment">% clusterdata shows how a clustering algorithm can be hooked in where the number of</span>
  240. 0187 <span class="comment">% classes need to be assigned.</span>
  241. 0188
  242. 0189 <span class="keyword">if</span> length(classification) == 1
  243. 0190 msgbox(<span class="string">'Error in clustering. Try changing the number of target clusters or rerunning.'</span>);
  244. 0191 <span class="keyword">else</span>
  245. 0192 <span class="keyword">if</span> diagnosticstate
  246. 0193 figure();
  247. 0194 [silh h] = silhouette(matrix2classify,classification);
  248. 0195 <a href="#_sub23" class="code" title="subfunction pcaplot(matrix2classify,classification)">pcaplot</a>(matrix2classify,classification);
  249. 0196 <span class="keyword">end</span>
  250. 0197 handles.classification = classification;
  251. 0198 <span class="keyword">end</span>
  252. 0199 <span class="keyword">else</span>
  253. 0200 <span class="keyword">return</span>;
  254. 0201 <span class="keyword">end</span>
  255. 0202 <span class="keyword">else</span>
  256. 0203 <span class="keyword">try</span>
  257. 0204 minclust = str2num(get(handles.MinClusters,<span class="string">'String'</span>));
  258. 0205 <span class="keyword">catch</span>
  259. 0206 <span class="keyword">return</span>;
  260. 0207 <span class="keyword">end</span>
  261. 0208 <span class="keyword">try</span>
  262. 0209 maxclust = str2num(get(handles.MaxClusters,<span class="string">'String'</span>));
  263. 0210 <span class="keyword">catch</span>
  264. 0211 <span class="keyword">return</span>;
  265. 0212 <span class="keyword">end</span>
  266. 0213
  267. 0214 resultsclust = cell((1+ maxclust)-minclust,1);
  268. 0215
  269. 0216 hw = waitbar(0,<span class="string">'Clustering data set '</span>);
  270. 0217 <span class="keyword">for</span> i = 1:(1+maxclust)-minclust
  271. 0218 resultsclust{i} = <a href="#_sub3" class="code" title="subfunction clusters = clusterdata(X,k,dmeasure)">clusterdata</a>(matrix2classify,minclust + (i-1),handles.distancemeasure);
  272. 0219 <span class="keyword">if</span> length(resultsclust{i}) == 1
  273. 0220 msgbox([<span class="string">'Clustering failed at '</span> num2str(resultsclust{i}) <span class="string">' clusters. Try changing the range of clusters or rerunning.'</span>]);
  274. 0221 <span class="keyword">return</span>;
  275. 0222 <span class="keyword">end</span>
  276. 0223 waitbar(i/(1+maxclust-minclust));
  277. 0224 <span class="keyword">end</span>
  278. 0225 <a href="../../chronux_2_10/fly_track/videoIO/videoIO_2006a/@videoReader/close.html" class="code" title="function vr = close(vr)">close</a>(hw);
  279. 0226
  280. 0227 silhmean = zeros((1+ maxclust)-minclust,1);
  281. 0228
  282. 0229 <span class="keyword">for</span> i = 1:1+(maxclust)-minclust
  283. 0230
  284. 0231 <span class="keyword">if</span> diagnosticstate
  285. 0232 figure();
  286. 0233 [silh h] = silhouette(matrix2classify,resultsclust{i});
  287. 0234 <a href="#_sub23" class="code" title="subfunction pcaplot(matrix2classify,classification)">pcaplot</a>(matrix2classify,resultsclust{i});
  288. 0235 <span class="keyword">else</span>
  289. 0236 silh = silhouette(matrix2classify,resultsclust{i});
  290. 0237 <span class="keyword">end</span>
  291. 0238
  292. 0239 silhmean(i) = mean(silh);
  293. 0240 <span class="keyword">end</span>
  294. 0241
  295. 0242 [x I] = max(silhmean);
  296. 0243 handles.classification = resultsclust{I(1)};
  297. 0244
  298. 0245 set(handles.KClasses,<span class="string">'String'</span>,num2str(minclust + (I(1)-1)));
  299. 0246 <span class="keyword">end</span>
  300. 0247 <span class="keyword">end</span>
  301. 0248 <span class="keyword">end</span>
  302. 0249
  303. 0250
  304. 0251 set(handles.AcceptButton,<span class="string">'Enable'</span>,<span class="string">'on'</span>);
  305. 0252
  306. 0253 guidata(gcbo,handles);
  307. 0254
  308. 0255 <span class="comment">% --- Executes on button press in CancelButton.</span>
  309. 0256 <a name="_sub5" href="#_subfunctions" class="code">function CancelButton_Callback(hObject, eventdata, handles)</a>
  310. 0257 <span class="comment">% hObject handle to CancelButton (see GCBO)</span>
  311. 0258 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  312. 0259 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  313. 0260
  314. 0261 handles.output = 0;
  315. 0262 guidata(hObject,handles);
  316. 0263 uiresume(handles.figure1);
  317. 0264
  318. 0265 <span class="comment">% --- Executes on button press in AcceptButton.</span>
  319. 0266 <a name="_sub6" href="#_subfunctions" class="code">function AcceptButton_Callback(hObject, eventdata, handles)</a>
  320. 0267 <span class="comment">% hObject handle to AcceptButton (see GCBO)</span>
  321. 0268 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  322. 0269 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  323. 0270
  324. 0271 handles.output = handles.classification;
  325. 0272 guidata(hObject,handles);
  326. 0273 uiresume(handles.figure1);
  327. 0274
  328. 0275
  329. 0276 <a name="_sub7" href="#_subfunctions" class="code">function CepstralEdit_Callback(hObject, eventdata, handles)</a>
  330. 0277 <span class="comment">% hObject handle to CepstralEdit (see GCBO)</span>
  331. 0278 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  332. 0279 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  333. 0280
  334. 0281 <span class="comment">% Hints: get(hObject,'String') returns contents of CepstralEdit as text</span>
  335. 0282 <span class="comment">% str2double(get(hObject,'String')) returns contents of CepstralEdit as a double</span>
  336. 0283
  337. 0284
  338. 0285 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  339. 0286 <a name="_sub8" href="#_subfunctions" class="code">function CepstralEdit_CreateFcn(hObject, eventdata, handles)</a>
  340. 0287 <span class="comment">% hObject handle to CepstralEdit (see GCBO)</span>
  341. 0288 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  342. 0289 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  343. 0290
  344. 0291 <span class="comment">% Hint: edit controls usually have a white background on Windows.</span>
  345. 0292 <span class="comment">% See ISPC and COMPUTER.</span>
  346. 0293 <span class="keyword">if</span> ispc
  347. 0294 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  348. 0295 <span class="keyword">else</span>
  349. 0296 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  350. 0297 <span class="keyword">end</span>
  351. 0298
  352. 0299
  353. 0300
  354. 0301 <a name="_sub9" href="#_subfunctions" class="code">function MinClusters_Callback(hObject, eventdata, handles)</a>
  355. 0302 <span class="comment">% hObject handle to MinClusters (see GCBO)</span>
  356. 0303 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  357. 0304 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  358. 0305
  359. 0306 <span class="comment">% Hints: get(hObject,'String') returns contents of MinClusters as text</span>
  360. 0307 <span class="comment">% str2double(get(hObject,'String')) returns contents of MinClusters as a double</span>
  361. 0308
  362. 0309
  363. 0310 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  364. 0311 <a name="_sub10" href="#_subfunctions" class="code">function MinClusters_CreateFcn(hObject, eventdata, handles)</a>
  365. 0312 <span class="comment">% hObject handle to MinClusters (see GCBO)</span>
  366. 0313 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  367. 0314 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  368. 0315
  369. 0316 <span class="comment">% Hint: edit controls usually have a white background on Windows.</span>
  370. 0317 <span class="comment">% See ISPC and COMPUTER.</span>
  371. 0318 <span class="keyword">if</span> ispc
  372. 0319 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  373. 0320 <span class="keyword">else</span>
  374. 0321 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  375. 0322 <span class="keyword">end</span>
  376. 0323
  377. 0324
  378. 0325
  379. 0326 <a name="_sub11" href="#_subfunctions" class="code">function MaxClusters_Callback(hObject, eventdata, handles)</a>
  380. 0327 <span class="comment">% hObject handle to MaxClusters (see GCBO)</span>
  381. 0328 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  382. 0329 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  383. 0330
  384. 0331 <span class="comment">% Hints: get(hObject,'String') returns contents of MaxClusters as text</span>
  385. 0332 <span class="comment">% str2double(get(hObject,'String')) returns contents of MaxClusters as a double</span>
  386. 0333
  387. 0334
  388. 0335 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  389. 0336 <a name="_sub12" href="#_subfunctions" class="code">function MaxClusters_CreateFcn(hObject, eventdata, handles)</a>
  390. 0337 <span class="comment">% hObject handle to MaxClusters (see GCBO)</span>
  391. 0338 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  392. 0339 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  393. 0340
  394. 0341 <span class="comment">% Hint: edit controls usually have a white background on Windows.</span>
  395. 0342 <span class="comment">% See ISPC and COMPUTER.</span>
  396. 0343 <span class="keyword">if</span> ispc
  397. 0344 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  398. 0345 <span class="keyword">else</span>
  399. 0346 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  400. 0347 <span class="keyword">end</span>
  401. 0348
  402. 0349
  403. 0350 <span class="comment">% --- Executes on selection change in TransformPopupMenu.</span>
  404. 0351 <a name="_sub13" href="#_subfunctions" class="code">function TransformPopupMenu_Callback(hObject, eventdata, handles)</a>
  405. 0352 <span class="comment">% hObject handle to TransformPopupMenu (see GCBO)</span>
  406. 0353 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  407. 0354 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  408. 0355
  409. 0356 <span class="comment">% Hints: contents = get(hObject,'String') returns TransformPopupMenu contents as cell array</span>
  410. 0357 <span class="comment">% contents{get(hObject,'Value')} returns selected item from TransformPopupMenu</span>
  411. 0358
  412. 0359 <span class="comment">% value = get(hObject,'Value');</span>
  413. 0360 <span class="comment">%</span>
  414. 0361 <span class="comment">% if value == 2</span>
  415. 0362 <span class="comment">% handles.matrix2classify(:,1) = log(handles.matrix2classify(:,1));</span>
  416. 0363 <span class="comment">% else</span>
  417. 0364 <span class="comment">% handles.matrix2classify(:,1) = exp(handles.matrix2classify(:,1));</span>
  418. 0365 <span class="comment">% end</span>
  419. 0366
  420. 0367 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  421. 0368 <a name="_sub14" href="#_subfunctions" class="code">function TransformPopupMenu_CreateFcn(hObject, eventdata, handles)</a>
  422. 0369 <span class="comment">% hObject handle to TransformPopupMenu (see GCBO)</span>
  423. 0370 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  424. 0371 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  425. 0372
  426. 0373 <span class="comment">% Hint: popupmenu controls usually have a white background on Windows.</span>
  427. 0374 <span class="comment">% See ISPC and COMPUTER.</span>
  428. 0375 <span class="keyword">if</span> ispc
  429. 0376 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  430. 0377 <span class="keyword">else</span>
  431. 0378 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  432. 0379 <span class="keyword">end</span>
  433. 0380
  434. 0381 <a name="_sub15" href="#_subfunctions" class="code">function KClasses_Callback(hObject, eventdata, handles)</a>
  435. 0382 <span class="comment">% hObject handle to KClasses (see GCBO)</span>
  436. 0383 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  437. 0384 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  438. 0385
  439. 0386 <span class="comment">% Hints: get(hObject,'String') returns contents of KClasses as text</span>
  440. 0387 <span class="comment">% str2double(get(hObject,'String')) returns contents of KClasses as a double</span>
  441. 0388
  442. 0389
  443. 0390 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  444. 0391 <a name="_sub16" href="#_subfunctions" class="code">function KClasses_CreateFcn(hObject, eventdata, handles)</a>
  445. 0392 <span class="comment">% hObject handle to KClasses (see GCBO)</span>
  446. 0393 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  447. 0394 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  448. 0395
  449. 0396 <span class="comment">% Hint: edit controls usually have a white background on Windows.</span>
  450. 0397 <span class="comment">% See ISPC and COMPUTER.</span>
  451. 0398 <span class="keyword">if</span> ispc
  452. 0399 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  453. 0400 <span class="keyword">else</span>
  454. 0401 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  455. 0402 <span class="keyword">end</span>
  456. 0403
  457. 0404
  458. 0405 <span class="comment">% --- Executes on selection change in CepstralPopupMenu.</span>
  459. 0406 <a name="_sub17" href="#_subfunctions" class="code">function CepstralPopupMenu_Callback(hObject, eventdata, handles)</a>
  460. 0407 <span class="comment">% hObject handle to CepstralPopupMenu (see GCBO)</span>
  461. 0408 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  462. 0409 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  463. 0410
  464. 0411 <span class="comment">% Hints: contents = get(hObject,'String') returns CepstralPopupMenu contents as cell array</span>
  465. 0412 <span class="comment">% contents{get(hObject,'Value')} returns selected item from CepstralPopupMenu</span>
  466. 0413
  467. 0414
  468. 0415 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  469. 0416 <a name="_sub18" href="#_subfunctions" class="code">function CepstralPopupMenu_CreateFcn(hObject, eventdata, handles)</a>
  470. 0417 <span class="comment">% hObject handle to CepstralPopupMenu (see GCBO)</span>
  471. 0418 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  472. 0419 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  473. 0420
  474. 0421 <span class="comment">% Hint: popupmenu controls usually have a white background on Windows.</span>
  475. 0422 <span class="comment">% See ISPC and COMPUTER.</span>
  476. 0423 <span class="keyword">if</span> ispc
  477. 0424 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  478. 0425 <span class="keyword">else</span>
  479. 0426 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  480. 0427 <span class="keyword">end</span>
  481. 0428
  482. 0429
  483. 0430
  484. 0431 <span class="comment">% --- Executes on button press in RangeSpecify.</span>
  485. 0432 <a name="_sub19" href="#_subfunctions" class="code">function RangeSpecify_Callback(hObject, eventdata, handles)</a>
  486. 0433 <span class="comment">% hObject handle to RangeSpecify (see GCBO)</span>
  487. 0434 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  488. 0435 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  489. 0436
  490. 0437 <span class="comment">% Hint: get(hObject,'Value') returns toggle state of RangeSpecify</span>
  491. 0438
  492. 0439 state = get(hObject,<span class="string">'Value'</span>);
  493. 0440
  494. 0441 <span class="keyword">if</span> state
  495. 0442 set(handles.KClasses,<span class="string">'Enable'</span>,<span class="string">'off'</span>);
  496. 0443 set(handles.text3,<span class="string">'Visible'</span>,<span class="string">'on'</span>);
  497. 0444 set(handles.MinClusters,<span class="string">'Visible'</span>,<span class="string">'on'</span>);
  498. 0445 set(handles.text4,<span class="string">'Visible'</span>,<span class="string">'on'</span>);
  499. 0446 set(handles.MaxClusters,<span class="string">'Visible'</span>,<span class="string">'on'</span>);
  500. 0447 <span class="keyword">else</span>
  501. 0448 set(handles.KClasses,<span class="string">'Enable'</span>,<span class="string">'on'</span>);
  502. 0449 set(handles.text3,<span class="string">'Visible'</span>,<span class="string">'off'</span>);
  503. 0450 set(handles.MinClusters,<span class="string">'Visible'</span>,<span class="string">'off'</span>);
  504. 0451 set(handles.text4,<span class="string">'Visible'</span>,<span class="string">'off'</span>);
  505. 0452 set(handles.MaxClusters,<span class="string">'Visible'</span>,<span class="string">'off'</span>);
  506. 0453 <span class="keyword">end</span>
  507. 0454
  508. 0455
  509. 0456 <span class="comment">% --- Executes on selection change in DistancePopupMenu.</span>
  510. 0457 <a name="_sub20" href="#_subfunctions" class="code">function DistancePopupMenu_Callback(hObject, eventdata, handles)</a>
  511. 0458 <span class="comment">% hObject handle to DistancePopupMenu (see GCBO)</span>
  512. 0459 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  513. 0460 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  514. 0461
  515. 0462 <span class="comment">% Hints: contents = get(hObject,'String') returns DistancePopupMenu contents as cell array</span>
  516. 0463 <span class="comment">% contents{get(hObject,'Value')} returns selected item from DistancePopupMenu</span>
  517. 0464
  518. 0465 contents = get(hObject,<span class="string">'String'</span>);
  519. 0466
  520. 0467 dstring = contents{get(hObject,<span class="string">'Value'</span>)};
  521. 0468
  522. 0469 <span class="keyword">switch</span> dstring
  523. 0470 <span class="keyword">case</span> <span class="string">'Squared Euclidean'</span>
  524. 0471 handles.distancemeasure = <span class="string">'sqEuclidean'</span>
  525. 0472 <span class="keyword">case</span> <span class="string">'City Block (L1)'</span>
  526. 0473 handles.distancemeasure = <span class="string">'cityblock'</span>
  527. 0474 <span class="keyword">case</span> <span class="string">'Cosine'</span>
  528. 0475 handles.distancemeasure = <span class="string">'cosine'</span>
  529. 0476 <span class="keyword">case</span> <span class="string">'Correlation'</span>
  530. 0477 handles.distancemeasure = <span class="string">'correlation'</span>
  531. 0478 <span class="keyword">end</span>
  532. 0479
  533. 0480 guidata(gcbo,handles);
  534. 0481
  535. 0482 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  536. 0483 <a name="_sub21" href="#_subfunctions" class="code">function DistancePopupMenu_CreateFcn(hObject, eventdata, handles)</a>
  537. 0484 <span class="comment">% hObject handle to DistancePopupMenu (see GCBO)</span>
  538. 0485 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  539. 0486 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  540. 0487
  541. 0488 <span class="comment">% Hint: popupmenu controls usually have a white background on Windows.</span>
  542. 0489 <span class="comment">% See ISPC and COMPUTER.</span>
  543. 0490 <span class="keyword">if</span> ispc
  544. 0491 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  545. 0492 <span class="keyword">else</span>
  546. 0493 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  547. 0494 <span class="keyword">end</span>
  548. 0495
  549. 0496 handles.distancemeasure = <span class="string">'sqEuclidean'</span>;
  550. 0497
  551. 0498 guidata(gcbo,handles);
  552. 0499
  553. 0500 <span class="comment">% --- Executes on button press in DiagnosticCheckbox.</span>
  554. 0501 <a name="_sub22" href="#_subfunctions" class="code">function DiagnosticCheckbox_Callback(hObject, eventdata, handles)</a>
  555. 0502 <span class="comment">% hObject handle to DiagnosticCheckbox (see GCBO)</span>
  556. 0503 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  557. 0504 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  558. 0505
  559. 0506 <span class="comment">% Hint: get(hObject,'Value') returns toggle state of DiagnosticCheckbox</span>
  560. 0507
  561. 0508 <a name="_sub23" href="#_subfunctions" class="code">function pcaplot(matrix2classify,classification)</a>
  562. 0509 <span class="comment">% Plots the first two principal components uses different shapes selected</span>
  563. 0510 <span class="comment">% randomly and different colors selected randomly to show classes.</span>
  564. 0511
  565. 0512 nclusters = length(unique(classification));
  566. 0513
  567. 0514 <span class="keyword">if</span> not(isempty(find(classification == 0)))
  568. 0515 nclusters = nclusters -1;
  569. 0516 <span class="keyword">end</span>
  570. 0517
  571. 0518 colors = rand(nclusters,3);
  572. 0519 mark = mod(1:nclusters,13)+1;
  573. 0520
  574. 0521 [coef, score] = princomp(matrix2classify);
  575. 0522
  576. 0523 figure();
  577. 0524
  578. 0525 <span class="keyword">for</span> i = 1:length(classification)
  579. 0526 h = line(score(i,1),score(i,2));
  580. 0527 markers = set(h,<span class="string">'Marker'</span>);
  581. 0528 <span class="keyword">if</span> classification(i) == 0 <span class="comment">% For segments which are not classified</span>
  582. 0529 set(h,<span class="string">'Marker'</span>,markers{4});
  583. 0530 set(h,<span class="string">'Color'</span>,[0 0 0]);
  584. 0531 <span class="keyword">else</span>
  585. 0532 set(h,<span class="string">'Marker'</span>,markers{mark(classification(i))});
  586. 0533 set(h,<span class="string">'Color'</span>,colors(classification(i),:));
  587. 0534 <span class="keyword">end</span>
  588. 0535 <span class="keyword">end</span>
  589. 0536
  590. 0537 xlabel(<span class="string">'PCA 1'</span>);
  591. 0538 ylabel(<span class="string">'PCA 2'</span>);
  592. 0539
  593. 0540 <a name="_sub24" href="#_subfunctions" class="code">function classifymatrix = madnormalize(classifymatrix, cols2normalize)</a>
  594. 0541 <span class="keyword">for</span> i = cols2normalize
  595. 0542 classifymatrix(:,i) = (classifymatrix(:,i) - median(classifymatrix(:,i))) / mad(classifymatrix(:,i));
  596. 0543 <span class="keyword">end</span>
  597. 0544
  598. 0545
  599. 0546 <span class="comment">% This code is modified from http://phys.columbia.edu/~aylin/clustering</span>
  600. 0547
  601. 0548 <a name="_sub25" href="#_subfunctions" class="code">function c=cluster_aylin(p1,p2);</a>
  602. 0549 <span class="comment">%enter the p1,p2 found from hneighbors.m and get the struct array c.</span>
  603. 0550 <span class="comment">%c(i).c will contain the indices of the objects that is in cluster i.</span>
  604. 0551 <span class="comment">%c(1).c will be the cluster that has the maximum number of objects.</span>
  605. 0552 nn=size(p1,2);
  606. 0553 p=std(p1);g=p(p2);p=p';
  607. 0554 z=zeros(nn,1);zz=zeros(nn,1);
  608. 0555 n=zeros(1,nn);
  609. 0556 <span class="keyword">for</span> i=1:nn
  610. 0557 n(i)=max(max(p1(:,p2(:,i))));
  611. 0558 <span class="keyword">end</span>
  612. 0559 gg=[1:nn; max(g); n]';n=find(n&lt;1);
  613. 0560 [q1,q1]=sort(gg(n,2)');
  614. 0561 g=gg(n(q1),1);
  615. 0562 t=cputime;
  616. 0563 j=1;m=1
  617. 0564 <span class="keyword">for</span> i=1:length(n)
  618. 0565 ii=g(i);b=p2(:,ii);a=b;aa=a(find(gg(a,2)&lt;1));
  619. 0566 <span class="keyword">if</span> length([0; unique(z(aa))])&lt;=2;a1=1;a2=0;
  620. 0567 <span class="keyword">while</span> a1~=a2;a1=length(aa);
  621. 0568 a=unique(p2(:,a));a=a(find(gg(a,2)&lt;1));
  622. 0569 a=unique(a(find(gg(a,2)&lt;=mean(gg(aa,2))+std(gg(aa,2)))));
  623. 0570 a=a(find(ismember(a,aa)==0));
  624. 0571 <span class="keyword">if</span> ~isempty(a);aa=[aa;a];
  625. 0572 jj=aa(find(z(aa)));
  626. 0573 <span class="keyword">if</span> ~isempty(jj);;
  627. 0574 u=unique(z(jj));
  628. 0575 <span class="keyword">if</span> length(u)==1;
  629. 0576 zz(aa(find(~z(aa))))=m;z(aa)=u;m=m+1;
  630. 0577 <span class="keyword">end</span>
  631. 0578 <span class="keyword">break</span>;
  632. 0579 <span class="keyword">end</span>
  633. 0580 <span class="keyword">end</span>;a2=length(aa);
  634. 0581 <span class="keyword">end</span>;a=aa;
  635. 0582 jj=a(find(z(a)));
  636. 0583 <span class="keyword">if</span> isempty(jj);
  637. 0584 z(a)=j;j=j+1;
  638. 0585 zz(a)=m;zz(b)=m-.1;zz(ii)=m-.2;m=m+1;
  639. 0586 <span class="keyword">end</span>
  640. 0587 <span class="keyword">end</span>
  641. 0588 <span class="keyword">end</span>
  642. 0589 u=unique(z)
  643. 0590 u=u(find(u));v=length(u);vv=floor(1/v);<span class="keyword">if</span> vv;vv=<span class="string">' is'</span>;<span class="keyword">else</span> vv=<span class="string">'s are'</span>;<span class="keyword">end</span>
  644. 0591 fprintf([int2str(v) <span class="string">' cluster'</span> vv <span class="string">' found in '</span> num2str(cputime-t) <span class="string">'sec\n\n'</span>])
  645. 0592 q0=[];<span class="keyword">for</span> i=1:v;qq=find(z==u(i));q0=[q0 length(qq)];<span class="keyword">end</span>;
  646. 0593 [q1,q2]=sort(q0);<span class="comment">%q2=q2(find(100*q1/nn&gt;1));v=length(q2);</span>
  647. 0594 c=[];<span class="keyword">for</span> i=1:v;qq=find(z==u(q2(i)));[j,j]=sort(zz(qq));c(v-i+1).c=qq(j);<span class="keyword">end</span>;
  648. 0595 <span class="keyword">if</span> isempty(c);fprintf(<span class="string">'\tNo cluster was found. \n \tscale the data (step size must be 1)\n'</span>);<span class="keyword">end</span>
  649. 0596
  650. 0597
  651. 0598 <a name="_sub26" href="#_subfunctions" class="code">function [p1,p2]=hneighbors(e);</a>
  652. 0599 <span class="comment">% this function finds the neighbors of each object in 'e' within a unit hypercube</span>
  653. 0600 <span class="comment">% and returns the sorted object distances to q.q1 and their identities to q.q2 , 'e' is a</span>
  654. 0601 <span class="comment">% matrix where i th row and j th column represents the j th component of the i th object.</span>
  655. 0602 s=<span class="string">'find('</span>;i=<span class="string">'abs(e(:,%d)-e(i,%d))&lt;1&amp;'</span>;
  656. 0603 <span class="comment">% assuming that the data is given scaled and the characteristic step size is 1, variable s</span>
  657. 0604 <span class="comment">% keeps a script to find the objects that lie within a unit hypercube around the i th object.</span>
  658. 0605 <span class="keyword">for</span> j=1:size(e,2);
  659. 0606 s=[s sprintf(i,j,j)];
  660. 0607 <span class="keyword">end</span>;s([end end+1])=<span class="string">');'</span>;
  661. 0608 <span class="comment">% runs the script s for each of the objects and stores the sorted distances</span>
  662. 0609 <span class="comment">% from the i th object in q(i).q1 and their indentities in q(i).q2</span>
  663. 0610 nn=size(e,1);m=ceil(nn^(1/4));p1=ones(m,nn);p2=kron(ones(m,1),1:nn);
  664. 0611 <span class="keyword">for</span> i=1:nn;
  665. 0612 j=eval(s);
  666. 0613 [q,qq]=sort(sqrt(sum((e(j,:)-kron(ones(length(j),1),e(i,:)))'.^2)));q=q(find(q&lt;1));mm=length(q);
  667. 0614 qq=j(qq(1:mm))';mn=min([m mm]);
  668. 0615 p1(1:mn,i)=q(1:mn)';
  669. 0616 p2(1:mn,i)=qq(1:mn)';
  670. 0617 <span class="keyword">end</span>
  671. 0618
  672. 0619
  673. 0620 <a name="_sub27" href="#_subfunctions" class="code">function classification = cluster_hierarchical(matrix2classify)</a>
  674. 0621 [p1,p2] = <a href="#_sub26" class="code" title="subfunction [p1,p2]=hneighbors(e);">hneighbors</a>(matrix2classify);
  675. 0622 c = <a href="#_sub25" class="code" title="subfunction c=cluster_aylin(p1,p2);">cluster_aylin</a>(p1,p2);
  676. 0623
  677. 0624 nclusters = size(c,2);
  678. 0625
  679. 0626 total_classified = 0
  680. 0627
  681. 0628 nsegments = size(matrix2classify,1);
  682. 0629
  683. 0630 classification = zeros(nsegments,1);
  684. 0631
  685. 0632 <span class="keyword">for</span> i = 1:nclusters
  686. 0633 <span class="keyword">for</span> j = 1:length(c(i).c)
  687. 0634 classification(c(i).c(j)) = i;
  688. 0635 <span class="keyword">end</span>
  689. 0636 <span class="keyword">end</span>
  690. 0637 ;
  691. 0638
  692. 0639
  693. 0640 <span class="comment">% --- Executes on button press in HierarchicalRadioButton.</span>
  694. 0641 <a name="_sub28" href="#_subfunctions" class="code">function HierarchicalRadioButton_Callback(hObject, eventdata, handles)</a>
  695. 0642 <span class="comment">% hObject handle to HierarchicalRadioButton (see GCBO)</span>
  696. 0643 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  697. 0644 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  698. 0645
  699. 0646 <span class="comment">% Hint: get(hObject,'Value') returns toggle state of HierarchicalRadioButton</span>
  700. 0647
  701. 0648 selected = get(hObject,<span class="string">'Value'</span>);
  702. 0649
  703. 0650 <span class="keyword">if</span> selected
  704. 0651 handles.cluster_method = <span class="string">'hierarchical'</span>;
  705. 0652 <span class="keyword">end</span>
  706. 0653
  707. 0654 guidata(gcbo,handles);
  708. 0655
  709. 0656
  710. 0657 <span class="comment">% --- Executes on button press in KmeansRadioButton.</span>
  711. 0658 <a name="_sub29" href="#_subfunctions" class="code">function KmeansRadioButton_Callback(hObject, eventdata, handles)</a>
  712. 0659 <span class="comment">% hObject handle to KmeansRadioButton (see GCBO)</span>
  713. 0660 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  714. 0661 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  715. 0662
  716. 0663 <span class="comment">% Hint: get(hObject,'Value') returns toggle state of KmeansRadioButton</span>
  717. 0664
  718. 0665 selected = get(hObject,<span class="string">'Value'</span>);
  719. 0666
  720. 0667 <span class="keyword">if</span> selected
  721. 0668 handles.cluster_method = <span class="string">'kmeans'</span>;
  722. 0669 <span class="keyword">end</span>
  723. 0670
  724. 0671 guidata(gcbo,handles);
  725. 0672
  726. 0673
  727. 0674
  728. 0675
  729. 0676 <a name="_sub30" href="#_subfunctions" class="code">function SegLenWeight_Callback(hObject, eventdata, handles)</a>
  730. 0677 <span class="comment">% hObject handle to SegLenWeight (see GCBO)</span>
  731. 0678 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  732. 0679 <span class="comment">% handles structure with handles and user data (see GUIDATA)</span>
  733. 0680
  734. 0681 <span class="comment">% Hints: get(hObject,'String') returns contents of SegLenWeight as text</span>
  735. 0682 <span class="comment">% str2double(get(hObject,'String')) returns contents of SegLenWeight as a double</span>
  736. 0683
  737. 0684
  738. 0685 <span class="comment">% --- Executes during object creation, after setting all properties.</span>
  739. 0686 <a name="_sub31" href="#_subfunctions" class="code">function SegLenWeight_CreateFcn(hObject, eventdata, handles)</a>
  740. 0687 <span class="comment">% hObject handle to SegLenWeight (see GCBO)</span>
  741. 0688 <span class="comment">% eventdata reserved - to be defined in a future version of MATLAB</span>
  742. 0689 <span class="comment">% handles empty - handles not created until after all CreateFcns called</span>
  743. 0690
  744. 0691 <span class="comment">% Hint: edit controls usually have a white background on Windows.</span>
  745. 0692 <span class="comment">% See ISPC and COMPUTER.</span>
  746. 0693 <span class="keyword">if</span> ispc
  747. 0694 set(hObject,<span class="string">'BackgroundColor'</span>,<span class="string">'white'</span>);
  748. 0695 <span class="keyword">else</span>
  749. 0696 set(hObject,<span class="string">'BackgroundColor'</span>,get(0,<span class="string">'defaultUicontrolBackgroundColor'</span>));
  750. 0697 <span class="keyword">end</span>
  751. 0698
  752. 0699</pre></div>
  753. <hr><address>Generated on Fri 12-Aug-2011 11:36:15 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
  754. </body>
  755. </html>