FAnalyze.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. function varargout = FAnalyze(varargin)
  2. % FANALYZE
  3. % For all your trajectory analysis needs! . See documentation for usage details.
  4. %Written by Dan Valente
  5. %November 2007
  6. % Begin initialization code - DO NOT EDIT
  7. gui_Singleton = 1;
  8. gui_State = struct('gui_Name', mfilename, ...
  9. 'gui_Singleton', gui_Singleton, ...
  10. 'gui_OpeningFcn', @FAnalyze_OpeningFcn, ...
  11. 'gui_OutputFcn', @FAnalyze_OutputFcn, ...
  12. 'gui_LayoutFcn', [] , ...
  13. 'gui_Callback', []);
  14. if nargin && ischar(varargin{1})
  15. gui_State.gui_Callback = str2func(varargin{1});
  16. end
  17. if nargout
  18. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  19. else
  20. gui_mainfcn(gui_State, varargin{:});
  21. end
  22. % End initialization code - DO NOT EDIT
  23. % --- Executes just before FAnalyze is made visible.
  24. function FAnalyze_OpeningFcn(hObject, eventdata, handles, varargin)
  25. % This function has no output args, see OutputFcn.
  26. % hObject handle to figure
  27. % eventdata reserved - to be defined in a future version of MATLAB
  28. % handles structure with handles and user data (see GUIDATA)
  29. % varargin command line arguments to FAnalyze (see VARARGIN)
  30. % Choose default command line output for FAnalyze
  31. handles.output = hObject;
  32. handles.called = 0;
  33. disp('Welcome to FAnalyze!')
  34. % Update handles structure
  35. guidata(hObject, handles);
  36. % UIWAIT makes FAnalyze wait for user response (see UIRESUME)
  37. % uiwait(handles.figure1);
  38. % --- Outputs from this function are returned to the command line.
  39. function varargout = FAnalyze_OutputFcn(hObject, eventdata, handles)
  40. % varargout cell array for returning output args (see VARARGOUT);
  41. % hObject handle to figure
  42. % eventdata reserved - to be defined in a future version of MATLAB
  43. % handles structure with handles and user data (see GUIDATA)
  44. % Get default command line output from handles structure
  45. varargout{1} = handles.output;
  46. % --- Executes on selection change in ws_vars.
  47. function ws_vars_Callback(hObject, eventdata, handles)
  48. % hObject handle to ws_vars (see GCBO)
  49. % eventdata reserved - to be defined in a future version of MATLAB
  50. % handles structure with handles and user data (see GUIDATA)
  51. % Hints: contents = get(hObject,'String') returns ws_vars contents as cell array
  52. % contents{get(hObject,'Value')} returns selected item from ws_vars
  53. % --- Executes during object creation, after setting all properties.
  54. function ws_vars_CreateFcn(hObject, eventdata, handles)
  55. % hObject handle to ws_vars (see GCBO)
  56. % eventdata reserved - to be defined in a future version of MATLAB
  57. % handles empty - handles not created until after all CreateFcns called
  58. % Hint: listbox controls usually have a white background on Windows.
  59. % See ISPC and COMPUTER.
  60. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  61. set(hObject,'BackgroundColor','white');
  62. end
  63. function bins_Callback(hObject, eventdata, handles)
  64. % hObject handle to bins (see GCBO)
  65. % eventdata reserved - to be defined in a future version of MATLAB
  66. % handles structure with handles and user data (see GUIDATA)
  67. % Hints: get(hObject,'String') returns contents of bins as text
  68. % str2double(get(hObject,'String')) returns contents of bins as a double
  69. % --- Executes during object creation, after setting all properties.
  70. function bins_CreateFcn(hObject, eventdata, handles)
  71. % hObject handle to bins (see GCBO)
  72. % eventdata reserved - to be defined in a future version of MATLAB
  73. % handles empty - handles not created until after all CreateFcns called
  74. % Hint: edit controls usually have a white background on Windows.
  75. % See ISPC and COMPUTER.
  76. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  77. set(hObject,'BackgroundColor','white');
  78. end
  79. % --- Executes on button press in view_dist.
  80. function view_dist_Callback(hObject, eventdata, handles)
  81. % hObject handle to view_dist (see GCBO)
  82. % eventdata reserved - to be defined in a future version of MATLAB
  83. % handles structure with handles and user data (see GUIDATA)
  84. handles.called = handles.called+1;
  85. index_selected = get(handles.ws_vars,'Value');
  86. bins = get(handles.bins,'String');
  87. phase_opt = get(get(handles.phase_space,'SelectedObject'), 'Tag');
  88. if (length(index_selected) == 1)
  89. var1 = get_var_names(handles);
  90. u = strfind(var1,'_');
  91. cur_zone1 = var1(u(1)+1:u(2)-1);
  92. cur_zone1 = strmatch(cur_zone1, handles.zone_names,'exact');
  93. cur_seg1 = var1(u(2)+1:end);
  94. cur_seg1 = strmatch(cur_seg1, handles.zone{cur_zone1}.seg_label,'exact');
  95. cur_var1 = var1(1:strfind(var1,'_')-1);
  96. if strcmp(phase_opt,'phase1D')
  97. P.label = var1;
  98. P.phase_opt = phase_opt;
  99. [P.data P.bins] = eval(['ProbDist1D(handles.zone{' num2str(cur_zone1) '}.' cur_var1 ...
  100. '{' num2str(cur_seg1) '}.data ,' bins ')']);
  101. elseif strcmp(phase_opt,'phase2D')
  102. P.label = var1;
  103. P.phase_opt = phase_opt;
  104. [P.data P.bins] = eval(['ProbDist2D(handles.zone{' num2str(cur_zone1) '}.' cur_var1...
  105. '{' num2str(cur_seg1) '}.data ,' bins ')']);
  106. end
  107. %plot the distribution
  108. figure
  109. plot(P.bins,P.data)
  110. elseif (length(index_selected) == 2)
  111. % should only look at joint distribution for same speed segments in
  112. % same zones right now. The JointDist command expects equal length
  113. % vectors. The following variables are just calculated in case we
  114. % modify JointDist down the road to handle vectors of different
  115. % lengths.
  116. if numel(str2num(bins)) == 2 | numel(str2num(bins))== 0
  117. [var1 var2] = get_var_names(handles);
  118. u1 = strfind(var1,'_');
  119. u2 = strfind(var2,'_');
  120. cur_zone1 = var1(u1(1)+1:u1(2)-1);
  121. cur_zone1 = strmatch(cur_zone1, handles.zone_names,'exact');
  122. cur_zone2 = var2(u2(1)+1:u2(2)-1);
  123. cur_zone2 = strmatch(cur_zone2, handles.zone_names,'exact');
  124. cur_seg1 = var1(u1(2)+1:end);
  125. cur_seg1 = strmatch(cur_seg1, handles.zone{cur_zone1}.seg_label,'exact');
  126. cur_seg2 = var2(u2(2)+1:end);
  127. cur_seg2 = strmatch(cur_seg2, handles.zone{cur_zone2}.seg_label,'exact');
  128. cur_var1 = var1(1:strfind(var1,'_')-1);
  129. cur_var2 = var2(1:strfind(var2,'_')-1);
  130. P.label = [var1 '_' var2];
  131. P.phase_opt = 'N/A';
  132. [P.data P.bins] = eval(['JointDist(handles.zone{' num2str(cur_zone1) '}.' cur_var1...
  133. '{' num2str(cur_seg1) '}.data , handles.zone{' num2str(cur_zone2) '}.' cur_var2...
  134. '{' num2str(cur_seg2) '}.data ,' bins ')']);
  135. figure
  136. imagesc(P.bins{2},P.bins{1},log(P.data))
  137. else
  138. errordlg('For a joint distribution, you must define bins or number of bins for BOTH directions!','Missing Bin Parameter')
  139. return;
  140. end
  141. elseif length(index_selected) > 2
  142. errordlg('You must select only one or two variables, no more than two!!!',...
  143. 'Incorrect Selection','modal')
  144. end
  145. if exist('P')
  146. handles.P{handles.called} = P;
  147. else
  148. handles.P{handles.called} = [];
  149. end
  150. guidata(gcbo,handles);
  151. % --- Executes on button press in segment_speed.
  152. function segment_speed_Callback(hObject, eventdata, handles)
  153. % hObject handle to segment_speed (see GCBO)
  154. % eventdata reserved - to be defined in a future version of MATLAB
  155. % handles structure with handles and user data (see GUIDATA)
  156. noise_thresh = str2num(get(handles.noise_thresh,'String'));
  157. NumZones = length(handles.zone_names);
  158. list1 = {'x','y','r','theta','vx','vy','v','vtheta','tau','kappa','beta'};
  159. fps = handles.fps;
  160. disp('Segmenting speed. Input parameters and wait...')
  161. %based on how many spatial zones, ask how we want to segment speed.
  162. %Remember that first zone is always the full arena.
  163. n=0;
  164. for i=1:NumZones
  165. prompt1 = {['How many speed segments in ' handles.zone{i}.zone_label{1}]};
  166. dlg_title1 = 'Segment Speed';
  167. num_lines1 = 1;
  168. def1 = {'2'};
  169. num_segs = str2double(inputdlg(prompt1,dlg_title1,num_lines1,def1));
  170. nameB4 = 'dummy';
  171. seg_names = [];
  172. speed_thresh = noise_thresh;
  173. for j=1:num_segs-1;
  174. prompt2 = {['Threshold between Segments ' num2str(j), ' and ' num2str(j+1) '.'],...
  175. 'Names for these zones:', ' '};
  176. dlg_title2 = 'Please Input Speed Segmentation Data';
  177. num_lines2 = 1;
  178. def2 = {'0.75','NZS','FSS'};
  179. answer = inputdlg(prompt2,dlg_title2,num_lines2,def2);
  180. speed_thresh = [speed_thresh str2double(answer(1))];
  181. nameA = answer(2);
  182. nameB = answer(3);
  183. check = strcmp(nameA,nameB4);
  184. if (j~=1 & ~check)
  185. errordlg([handles.zone{i}.zone_label{1} ' Segment ' num2str(j)...
  186. ' name must be same as previous ' handles.zone{i}.zone_label{1} ' Segment '...
  187. num2str(j) ' name! Rename!'])
  188. return;
  189. else
  190. seg_names = [seg_names nameA nameB];
  191. end
  192. nameB4 = nameB;
  193. end
  194. if (num_segs == 1 | num_segs == 2)
  195. seg_names = seg_names;
  196. elseif (num_segs== 3)
  197. seg_names = [seg_names(1:2) seg_names(end)];
  198. else
  199. seg_names = [seg_names(1:2) seg_names(end-1:end)];
  200. end
  201. %check speed thresholds in that zone
  202. [s indx] = sort(speed_thresh,'ascend');
  203. if (indx ~= [1:length(speed_thresh)])
  204. errordlg(['Speed thresholds for successive segments should be larger than previous segments. Try Again!'])
  205. return;
  206. elseif (~isempty(find(s < noise_thresh)))
  207. errordlg(['None of the thresholds are permitted to be below the noise threshold. Try Again!'])
  208. return;
  209. end
  210. seg_names = [{'all'} {'stops'} seg_names];
  211. for q=1:length(seg_names)
  212. for j=1:length(list1)
  213. if (strcmp(seg_names{q},'all') & strcmp(list1{j},'kappa')) | ...
  214. (strcmp(seg_names{q},'all') & strcmp(list1{j},'tau'))
  215. temp_list{j,q+n*length(seg_names)} = [];
  216. elseif (strcmp(seg_names{q},'stops') & strcmp(list1{j},'kappa')) | ...
  217. (strcmp(seg_names{q},'stops') & strcmp(list1{j},'beta'))
  218. temp_list{j,q+n*length(seg_names)} = [];
  219. elseif ~strcmp(seg_names{q},'all') & strcmp(list1{j},'beta')
  220. temp_list{j,q+n*length(seg_names)} = [];
  221. else
  222. temp_list{j,q+n*length(seg_names)} = [list1{j} '_' handles.zone{i}.zone_label{1} '_' seg_names{q}];
  223. end
  224. end
  225. end
  226. %calculate stuff for this particular zone
  227. t = handles.zone{i}.t{1}.data;
  228. x = handles.zone{i}.x{1}.data;
  229. y = handles.zone{i}.y{1}.data;
  230. r = handles.zone{i}.r{1}.data;
  231. theta = handles.zone{i}.theta{1}.data;
  232. vx = handles.zone{i}.vx{1}.data;
  233. vy = handles.zone{i}.vy{1}.data;
  234. v = handles.zone{i}.v{1}.data;
  235. vtheta =handles.zone{i}.vtheta{1}.data;
  236. stops_indx = find(v < speed_thresh(1));
  237. moves_indx = find(v >= speed_thresh(1));
  238. v(stops_indx) = 0; vx(stops_indx) = 0; vy(stops_indx) = 0; vtheta = atan2(vy,vx);
  239. handles.zone{i}.seg_label = seg_names;
  240. handles.zone{i}.t{2}.data = t(stops_indx);
  241. handles.zone{i}.x{2}.data = x(stops_indx);
  242. handles.zone{i}.y{2}.data = y(stops_indx);
  243. handles.zone{i}.r{2}.data = r(stops_indx);
  244. handles.zone{i}.theta{2}.data = theta(stops_indx);
  245. handles.zone{i}.vx{2}.data = vx(stops_indx);
  246. handles.zone{i}.vy{2}.data = vy(stops_indx);
  247. handles.zone{i}.v{2}.data = v(stops_indx);
  248. handles.zone{i}.vtheta{2}.data = vtheta(stops_indx);
  249. handles.zone{i}.tau{2}.data = FindDuration(stops_indx);
  250. handles.zone{i}.kappa{2}.data = 'N/A';
  251. for j=2:length(speed_thresh)
  252. indxA = find(v >= speed_thresh(j-1) & v < speed_thresh(j));
  253. indxB = find(v >= speed_thresh(j));
  254. handles.zone{i}.t{j+1}.data = t(indxA);
  255. handles.zone{i}.x{j+1}.data = x(indxA);
  256. handles.zone{i}.y{j+1}.data = y(indxA);
  257. handles.zone{i}.r{j+1}.data = r(indxA);
  258. handles.zone{i}.theta{j+1}.data = theta(indxA);
  259. handles.zone{i}.vx{j+1}.data = vx(indxA);
  260. handles.zone{i}.vy{j+1}.data = vy(indxA);
  261. handles.zone{i}.v{j+1}.data = v(indxA);
  262. handles.zone{i}.vtheta{j+1}.data = vtheta(indxA);
  263. handles.zone{i}.tau{j+1}.data = FindDuration(indxA);
  264. handles.zone{i}.kappa{j+1}.data = CalcCurvature(v, vtheta, indxA, 1/fps);
  265. handles.zone{i}.t{j+2}.data = t(indxB);
  266. handles.zone{i}.x{j+2}.data = x(indxB);
  267. handles.zone{i}.y{j+2}.data = y(indxB);
  268. handles.zone{i}.r{j+2}.data = r(indxB);
  269. handles.zone{i}.theta{j+2}.data = theta(indxB);
  270. handles.zone{i}.vx{j+2}.data = vx(indxB);
  271. handles.zone{i}.vy{j+2}.data = vy(indxB);
  272. handles.zone{i}.v{j+2}.data = v(indxB);
  273. handles.zone{i}.vtheta{j+2}.data = vtheta(indxB);
  274. handles.zone{i}.tau{j+2}.data = FindDuration(indxB);
  275. handles.zone{i}.kappa{j+2}.data = CalcCurvature(v, vtheta, indxB, 1/fps);
  276. end
  277. handles.zone{i}.beta{1}.data = CalcReorientAngle(vtheta, moves_indx);
  278. n=n+1;
  279. end
  280. temp3 = {};
  281. sz = size(temp_list);
  282. for q=1:sz(2)
  283. temp3 = [temp3 temp_list{:,q}];
  284. end
  285. disp('Speed has been segmented.')
  286. update_listbox(handles, temp3)
  287. guidata(gcbo,handles);
  288. % --- Executes on button press in load_traj.
  289. function load_traj_Callback(hObject, eventdata, handles)
  290. % hObject handle to load_traj (see GCBO)
  291. % eventdata reserved - to be defined in a future version of MATLAB
  292. % handles structure with handles and user data (see GUIDATA)
  293. [filename, pathname] = uigetfile({'*.mat'}, 'Select the .mat file containing the trajectory', 'MultiSelect','off');
  294. if isequal(filename,0) || isequal(pathname,0)
  295. disp('File select canceled')
  296. return;
  297. else
  298. disp(['File selected: ', fullfile(pathname, filename)])
  299. load(fullfile(pathname,filename));
  300. handles.filename = fullfile(pathname, filename);
  301. end
  302. handles.zone{1}.t{1}.data = t;
  303. handles.fps = 1/(t(2)-t(1));
  304. handles.zone{1}.x{1}.data = x;
  305. handles.zone{1}.y{1}.data = y;
  306. guidata(gcbo,handles);
  307. % --- Executes on button press in smooth.
  308. function smooth_Callback(hObject, eventdata, handles)
  309. % hObject handle to smooth (see GCBO)
  310. % eventdata reserved - to be defined in a future version of MATLAB
  311. % handles structure with handles and user data (see GUIDATA)
  312. %grab data
  313. t = handles.zone{1}.t{1}.data;
  314. fps = handles.fps;
  315. x = handles.zone{1}.x{1}.data;
  316. y = handles.zone{1}.y{1}.data;
  317. handles.P = [];
  318. %grab smoothing parameters as input by the user
  319. n = str2double(get(handles.n,'String'));
  320. dn = str2double(get(handles.dn,'String'));
  321. disp('Smoothing data. Please wait...')
  322. %smooth using runline
  323. x = runline(x, n, dn);
  324. y = runline(y, n, dn);
  325. %Calculate polar coords and velocity.
  326. r = sqrt(x.^2+y.^2);
  327. theta = atan2(y,x);
  328. vx = fps*gradient(x);
  329. vy = fps*gradient(y);
  330. v = sqrt(vx.^2+vy.^2);
  331. vtheta = atan2(vy,vx);
  332. clear handles.x;
  333. clear handles.y;
  334. %save smooth data to handles to be used by other GUI functions
  335. handles.zone_names{1} = 'Full Arena';
  336. handles.zone{1}.zone_label = {'Full Arena'};
  337. handles.zone{1}.seg_label = {'all'};
  338. handles.zone{1}.t{1}.data = t;
  339. handles.zone{1}.x{1}.data = x;
  340. handles.zone{1}.y{1}.data = y;
  341. handles.zone{1}.r{1}.data = r;
  342. handles.zone{1}.theta{1}.data = theta;
  343. handles.zone{1}.vx{1}.data = vx;
  344. handles.zone{1}.vy{1}.data = vy;
  345. handles.zone{1}.v{1}.data = v;
  346. handles.zone{1}.vtheta{1}.data = vtheta;
  347. handles.zone{1}.tau{1}.data = 'N/A';
  348. handles.zone{1}.kappa{1}.data = 'N/A';
  349. disp('Trajectory has been smoothed.')
  350. update_listbox(handles, {'x_Full Arena_all','y_Full Arena_all','r_Full Arena_all','theta_Full Arena_all',...
  351. 'vx_Full Arena_all','vy_Full Arena_all','v_Full Arena_all','vtheta_Full Arena_all'});
  352. guidata(gcbo,handles);
  353. function n_Callback(hObject, eventdata, handles)
  354. % hObject handle to n (see GCBO)
  355. % eventdata reserved - to be defined in a future version of MATLAB
  356. % handles structure with handles and user data (see GUIDATA)
  357. % Hints: get(hObject,'String') returns contents of n as text
  358. % str2double(get(hObject,'String')) returns contents of n as a double
  359. % --- Executes during object creation, after setting all properties.
  360. function n_CreateFcn(hObject, eventdata, handles)
  361. % hObject handle to n (see GCBO)
  362. % eventdata reserved - to be defined in a future version of MATLAB
  363. % handles empty - handles not created until after all CreateFcns called
  364. % Hint: edit controls usually have a white background on Windows.
  365. % See ISPC and COMPUTER.
  366. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  367. set(hObject,'BackgroundColor','white');
  368. end
  369. function dn_Callback(hObject, eventdata, handles)
  370. % hObject handle to dn (see GCBO)
  371. % eventdata reserved - to be defined in a future version of MATLAB
  372. % handles structure with handles and user data (see GUIDATA)
  373. % Hints: get(hObject,'String') returns contents of dn as text
  374. % str2double(get(hObject,'String')) returns contents of dn as a double
  375. % --- Executes during object creation, after setting all properties.
  376. function dn_CreateFcn(hObject, eventdata, handles)
  377. % hObject handle to dn (see GCBO)
  378. % eventdata reserved - to be defined in a future version of MATLAB
  379. % handles empty - handles not created until after all CreateFcns called
  380. % Hint: edit controls usually have a white background on Windows.
  381. % See ISPC and COMPUTER.
  382. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  383. set(hObject,'BackgroundColor','white');
  384. end
  385. % --- Executes on button press in view_traj.
  386. function view_traj_Callback(hObject, eventdata, handles)
  387. % hObject handle to view_traj (see GCBO)
  388. % eventdata reserved - to be defined in a future version of MATLAB
  389. % handles structure with handles and user data (see GUIDATA)
  390. t = handles.zone{1}.t{1}.data;
  391. x = handles.zone{1}.x{1}.data;
  392. y = handles.zone{1}.y{1}.data;
  393. tmp = getfield(handles, 'zone');
  394. if (isfield(tmp{1},'r'))
  395. r = handles.zone{1}.r{1}.data;
  396. theta = handles.zone{1}.theta{1}.data;
  397. vx = handles.zone{1}.vx{1}.data;
  398. vy = handles.zone{1}.vy{1}.data;
  399. v = handles.zone{1}.v{1}.data;
  400. vtheta =handles.zone{1}.vtheta{1}.data;
  401. end
  402. str = get(handles.variables, 'String');
  403. val = get(handles.variables, 'Value');
  404. % Set current data to the selected data set.
  405. if strcmp(str(val),'(x,y)')
  406. figure
  407. plot(x,y)
  408. xlabel('x-position')
  409. ylabel('y-position')
  410. title('Trajectory')
  411. if (~isfield(tmp{1},'r'))
  412. disp('You can view your trajectory, but you MUST smooth your data before proceeding with any calculations!')
  413. end
  414. else
  415. var1 = 't';
  416. if exist(str{val})
  417. figure
  418. var2 = str{val};
  419. eval(['plot(' var1 ',' var2 ')'])
  420. xlabel('Time')
  421. ylabel(var2)
  422. xlim([0 t(end)])
  423. else
  424. errordlg('Can"t view this variable unless data is smoothed','Non-existant Variable')
  425. return;
  426. end
  427. end
  428. % --- Executes on selection change in variables.
  429. function variables_Callback(hObject, eventdata, handles)
  430. % hObject handle to variables (see GCBO)
  431. % eventdata reserved - to be defined in a future version of MATLAB
  432. % handles structure with handles and user data (see GUIDATA)
  433. % Hints: contents = get(hObject,'String') returns variables contents as cell array
  434. % contents{get(hObject,'Value')} returns selected item from variables
  435. set(hObject,'String',{'(x,y)','x','y','r','theta','vx','vy','v','vtheta'})
  436. guidata(gcbo,handles);
  437. % --- Executes during object creation, after setting all properties.
  438. function variables_CreateFcn(hObject, eventdata, handles)
  439. % hObject handle to variables (see GCBO)
  440. % eventdata reserved - to be defined in a future version of MATLAB
  441. % handles empty - handles not created until after all CreateFcns called
  442. % Hint: popupmenu controls usually have a white background on Windows.
  443. % See ISPC and COMPUTER.
  444. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  445. set(hObject,'BackgroundColor','white');
  446. end
  447. set(hObject,'String',{'(x,y)','x','y','r','theta','vx','vy','v','vtheta'})
  448. guidata(gcbo,handles);
  449. % --- Executes on button press in segment_space.
  450. function segment_space_Callback(hObject, eventdata, handles)
  451. % hObject handle to segment_space (see GCBO)
  452. % eventdata reserved - to be defined in a future version of MATLAB
  453. % handles structure with handles and user data (see GUIDATA)
  454. NumZones = str2double(get(handles.num_zones,'String'));
  455. if NumZones < 2
  456. errordlg('Must have at least 2 zones','Too few zones')
  457. return;
  458. end
  459. zone_indx = 1:NumZones;
  460. list1 = {'x','y','r','theta','vx','vy','v','vtheta','tau','kappa'};
  461. zone_names = handles.zone_names;
  462. handles.zone_names = {};
  463. handles.zone_names{1} = 'Full Arena';
  464. t = handles.zone{1}.t{1}.data;
  465. dt = t(2)-t(1);
  466. x = handles.zone{1}.x{1}.data;
  467. y = handles.zone{1}.y{1}.data;
  468. r = handles.zone{1}.r{1}.data;
  469. theta = handles.zone{1}.theta{1}.data;
  470. vx = handles.zone{1}.vx{1}.data;
  471. vy = handles.zone{1}.vy{1}.data;
  472. v = handles.zone{1}.v{1}.data;
  473. vtheta =handles.zone{1}.vtheta{1}.data;
  474. disp('Segmenting Space. Input parameters and wait...')
  475. thresh(1) = 0;
  476. for i=1:NumZones-1
  477. prompt = {['Threshold between Zones ' num2str(zone_indx(i)), ' and ' num2str(zone_indx(i+1)) '.'],...
  478. 'Names for these zones:', ' '};
  479. dlg_title = 'Please Input Spatial Zone Data';
  480. num_lines = 1;
  481. def = {'7.3','CZ','RZ'};
  482. answer{i} = inputdlg(prompt,dlg_title,num_lines,def);
  483. thresh(i+1) = str2double(answer{i}(1));
  484. end
  485. zone_names = [zone_names answer{1}(2)];
  486. for i=1:NumZones-2
  487. nameA = answer{i}(2);
  488. nameB = answer{i}(3);
  489. nameC = answer{i+1}(2);
  490. if (~strcmp(nameB,nameC))
  491. errordlg(['Zone ' num2str(i+1) ' name must be same as previous Zone ' num2str(i+1) ' name! Rename!'])
  492. else
  493. zone_names = [zone_names nameB];
  494. end
  495. end
  496. zone_names = [zone_names answer{NumZones-1}(3)];
  497. for i=1:length(zone_names)
  498. for j=1:length(list1)
  499. if strcmp(zone_names{i},'Full Arena') & (strcmp(list1{j},'kappa') | strcmp(list1{j},'tau'))
  500. temp_list{j,i} = [];
  501. elseif strcmp(list1{j},'kappa')
  502. temp_list{j,i} = [];
  503. else
  504. temp_list{j,i} = [list1{j} '_' zone_names{i} '_all'];
  505. end
  506. end
  507. end
  508. temp3 = {};
  509. for i=1:NumZones+1
  510. temp3 = [temp3 temp_list{:,i}];
  511. end
  512. handles.zone_names = zone_names;
  513. %Now using thresholds, divvy up space
  514. for i = 2:length(thresh)
  515. % Can only do radial zones in this version of FAnalyze
  516. indxA = find(r >= thresh(i-1) & r < thresh(i));
  517. indxB = find(r >= thresh(i));
  518. handles.zone{i}.zone_label = zone_names(i);
  519. handles.zone{i}.seg_label = {'all'};
  520. handles.zone{i}.t{1}.data = t(indxA);
  521. handles.zone{i}.x{1}.data = x(indxA);
  522. handles.zone{i}.y{1}.data = y(indxA);
  523. handles.zone{i}.r{1}.data = r(indxA);
  524. handles.zone{i}.theta{1}.data = theta(indxA);
  525. handles.zone{i}.vx{1}.data = vx(indxA);
  526. handles.zone{i}.vy{1}.data = vy(indxA);
  527. handles.zone{i}.v{1}.data = v(indxA);
  528. handles.zone{i}.vtheta{1}.data = vtheta(indxA);
  529. handles.zone{i}.tau{1}.data = FindDuration(indxA);
  530. handles.zone{i}.kappa{1}.data = CalcCurvature(v, vtheta, indxA, dt);
  531. handles.zone{i+1}.zone_label = zone_names(i+1);
  532. handles.zone{i+1}.seg_label = {'all'};
  533. handles.zone{i+1}.t{1}.data = t(indxB);
  534. handles.zone{i+1}.x{1}.data = x(indxB);
  535. handles.zone{i+1}.y{1}.data = y(indxB);
  536. handles.zone{i+1}.r{1}.data = r(indxB);
  537. handles.zone{i+1}.theta{1}.data = theta(indxB);
  538. handles.zone{i+1}.vx{1}.data = vx(indxB);
  539. handles.zone{i+1}.vy{1}.data = vy(indxB);
  540. handles.zone{i+1}.v{1}.data = v(indxB);
  541. handles.zone{i+1}.vtheta{1}.data = vtheta(indxB);
  542. handles.zone{i+1}.tau{1}.data = FindDuration(indxB);
  543. handles.zone{i+1}.kappa{1}.data = CalcCurvature(v, vtheta, indxB, dt);
  544. end
  545. update_listbox(handles, temp3)
  546. disp('Space has been segmented.')
  547. guidata(gcbo,handles);
  548. % --- Executes on button press in save_ws.
  549. function save_ws_Callback(hObject, eventdata, handles)
  550. % hObject handle to save_ws (see GCBO)
  551. % eventdata reserved - to be defined in a future version of MATLAB
  552. % handles structure with handles and user data (see GUIDATA)
  553. % %This whole function just defines variables and saves the workspace as a
  554. % %.mat file.
  555. [filename, pathname] = uiputfile('*.mat', 'Pick a MAT file to save to');
  556. traj = handles.zone;
  557. P = handles.P;
  558. fps = handles.fps;
  559. save(fullfile(pathname, filename),'traj','P')
  560. disp(['Data has been saved to ' fullfile(pathname, filename)])
  561. function update_listbox(handles, vars)
  562. % this function updates the message center at the bottom of the GUI
  563. % adapted from Mike Rieser's PControl GUI.
  564. set(handles.ws_vars, 'String', vars);
  565. %%%%%%%%%%%%%%%%
  566. function varargout = get_var_names(handles)
  567. list_entries = get(handles.ws_vars,'String');
  568. index_selected = get(handles.ws_vars,'Value');
  569. varargout = list_entries(index_selected);
  570. function num_zones_Callback(hObject, eventdata, handles)
  571. % hObject handle to num_zones (see GCBO)
  572. % eventdata reserved - to be defined in a future version of MATLAB
  573. % handles structure with handles and user data (see GUIDATA)
  574. % Hints: get(hObject,'String') returns contents of num_zones as text
  575. % str2double(get(hObject,'String')) returns contents of num_zones as a double
  576. % --- Executes during object creation, after setting all properties.
  577. function num_zones_CreateFcn(hObject, eventdata, handles)
  578. % hObject handle to num_zones (see GCBO)
  579. % eventdata reserved - to be defined in a future version of MATLAB
  580. % handles empty - handles not created until after all CreateFcns called
  581. % Hint: edit controls usually have a white background on Windows.
  582. % See ISPC and COMPUTER.
  583. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  584. set(hObject,'BackgroundColor','white');
  585. end
  586. function noise_thresh_Callback(hObject, eventdata, handles)
  587. % hObject handle to noise_thresh (see GCBO)
  588. % eventdata reserved - to be defined in a future version of MATLAB
  589. % handles structure with handles and user data (see GUIDATA)
  590. % Hints: get(hObject,'String') returns contents of noise_thresh as text
  591. % str2double(get(hObject,'String')) returns contents of noise_thresh as a double
  592. % --- Executes during object creation, after setting all properties.
  593. function noise_thresh_CreateFcn(hObject, eventdata, handles)
  594. % hObject handle to noise_thresh (see GCBO)
  595. % eventdata reserved - to be defined in a future version of MATLAB
  596. % handles empty - handles not created until after all CreateFcns called
  597. % Hint: edit controls usually have a white background on Windows.
  598. % See ISPC and COMPUTER.
  599. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  600. set(hObject,'BackgroundColor','white');
  601. end