# FAnalyze

## PURPOSE

FANALYZE

## SYNOPSIS

function varargout = FAnalyze(varargin)

## DESCRIPTION

     FANALYZE
     For all your trajectory analysis needs! . See documentation for usage details.

## CROSS-REFERENCE INFORMATION

This function calls:

- CalcCurvature CALCCURVATURE
- CalcReorientAngle CALCREORIENTANGLE
- FindDuration FINDDURATION
- runline Running line fit (local linear regression)
- runline Running line fit (local linear regression)

This function is called by:

## SUBFUNCTIONS

- function FAnalyze_OpeningFcn(hObject, eventdata, handles, varargin)
- function varargout = FAnalyze_OutputFcn(hObject, eventdata, handles)
- function ws_vars_Callback(hObject, eventdata, handles)
- function ws_vars_CreateFcn(hObject, eventdata, handles)
- function bins_Callback(hObject, eventdata, handles)
- function bins_CreateFcn(hObject, eventdata, handles)
- function view_dist_Callback(hObject, eventdata, handles)
- function segment_speed_Callback(hObject, eventdata, handles)
- function load_traj_Callback(hObject, eventdata, handles)
- function smooth_Callback(hObject, eventdata, handles)
- function n_Callback(hObject, eventdata, handles)
- function n_CreateFcn(hObject, eventdata, handles)
- function dn_Callback(hObject, eventdata, handles)
- function dn_CreateFcn(hObject, eventdata, handles)
- function view_traj_Callback(hObject, eventdata, handles)
- function variables_Callback(hObject, eventdata, handles)
- function variables_CreateFcn(hObject, eventdata, handles)
- function segment_space_Callback(hObject, eventdata, handles)
- function save_ws_Callback(hObject, eventdata, handles)
- function update_listbox(handles, vars)
- function varargout = get_var_names(handles)
- function num_zones_Callback(hObject, eventdata, handles)
- function num_zones_CreateFcn(hObject, eventdata, handles)
- function noise_thresh_Callback(hObject, eventdata, handles)
- function noise_thresh_CreateFcn(hObject, eventdata, handles)

## SOURCE CODE

    0001 function varargout = FAnalyze(varargin)
    0002 % FANALYZE
    0003 % For all your trajectory analysis needs! . See documentation for usage details.
    0004
    0005 %Written by Dan Valente
    0006 %November 2007
    0007
    0008 % Begin initialization code - DO NOT EDIT
    0009 gui_Singleton = 1;
    0010 gui_State = struct('gui_Name',       mfilename, ...
    0011                    'gui_Singleton',  gui_Singleton, ...
    0012                    'gui_OpeningFcn', @FAnalyze_OpeningFcn, ...
    0013                    'gui_OutputFcn',  @FAnalyze_OutputFcn, ...
    0014                    'gui_LayoutFcn',  [] , ...
    0015                    'gui_Callback',   []);
    0016 if nargin && ischar(varargin{1})
    0017     gui_State.gui_Callback = str2func(varargin{1});
    0018 end
    0019
    0020 if nargout
    0021     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    0022 else
    0023     gui_mainfcn(gui_State, varargin{:});
    0024 end
    0025 % End initialization code - DO NOT EDIT
    0026
    0027
    0028 % --- Executes just before FAnalyze is made visible.
    0029 function FAnalyze_OpeningFcn(hObject, eventdata, handles, varargin)
    0030 % This function has no output args, see OutputFcn.
    0031 % hObject    handle to figure
    0032 % eventdata  reserved - to be defined in a future version of MATLAB
    0033 % handles    structure with handles and user data (see GUIDATA)
    0034 % varargin   command line arguments to FAnalyze (see VARARGIN)
    0035
    0036 % Choose default command line output for FAnalyze
    0037 handles.output = hObject;
    0038 handles.called = 0;
    0039 disp('Welcome to FAnalyze!')
    0040 % Update handles structure
    0041 guidata(hObject, handles);
    0042
    0043 % UIWAIT makes FAnalyze wait for user response (see UIRESUME)
    0044 % uiwait(handles.figure1);
    0045
    0046
    0047 % --- Outputs from this function are returned to the command line.
    0048 function varargout = FAnalyze_OutputFcn(hObject, eventdata, handles)
    0049 % varargout  cell array for returning output args (see VARARGOUT);
    0050 % hObject    handle to figure
    0051 % eventdata  reserved - to be defined in a future version of MATLAB
    0052 % handles    structure with handles and user data (see GUIDATA)
    0053
    0054 % Get default command line output from handles structure
    0055 varargout{1} = handles.output;
    0056
    0057
    0058 % --- Executes on selection change in ws_vars.
    0059 function ws_vars_Callback(hObject, eventdata, handles)
    0060 % hObject    handle to ws_vars (see GCBO)
    0061 % eventdata  reserved - to be defined in a future version of MATLAB
    0062 % handles    structure with handles and user data (see GUIDATA)
    0063
    0064 % Hints: contents = get(hObject,'String') returns ws_vars contents as cell array
    0065 %        contents{get(hObject,'Value')} returns selected item from ws_vars
    0066
    0067
    0068 % --- Executes during object creation, after setting all properties.
    0069 function ws_vars_CreateFcn(hObject, eventdata, handles)
    0070 % hObject    handle to ws_vars (see GCBO)
    0071 % eventdata  reserved - to be defined in a future version of MATLAB
    0072 % handles    empty - handles not created until after all CreateFcns called
    0073
    0074 % Hint: listbox controls usually have a white background on Windows.
    0075 %       See ISPC and COMPUTER.
    0076 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0077     set(hObject,'BackgroundColor','white');
    0078 end
    0079
    0080
    0081
    0082 function bins_Callback(hObject, eventdata, handles)
    0083 % hObject    handle to bins (see GCBO)
    0084 % eventdata  reserved - to be defined in a future version of MATLAB
    0085 % handles    structure with handles and user data (see GUIDATA)
    0086
    0087 % Hints: get(hObject,'String') returns contents of bins as text
    0088 %        str2double(get(hObject,'String')) returns contents of bins as a double
    0089
    0090
    0091 % --- Executes during object creation, after setting all properties.
    0092 function bins_CreateFcn(hObject, eventdata, handles)
    0093 % hObject    handle to bins (see GCBO)
    0094 % eventdata  reserved - to be defined in a future version of MATLAB
    0095 % handles    empty - handles not created until after all CreateFcns called
    0096
    0097 % Hint: edit controls usually have a white background on Windows.
    0098 %       See ISPC and COMPUTER.
    0099 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0100     set(hObject,'BackgroundColor','white');
    0101 end
    0102
    0103
    0104 % --- Executes on button press in view_dist.
    0105 function view_dist_Callback(hObject, eventdata, handles)
    0106 % hObject    handle to view_dist (see GCBO)
    0107 % eventdata  reserved - to be defined in a future version of MATLAB
    0108 % handles    structure with handles and user data (see GUIDATA)
    0109
    0110 handles.called = handles.called+1;
    0111 index_selected = get(handles.ws_vars,'Value');
    0112 bins = get(handles.bins,'String');
    0113 phase_opt = get(get(handles.phase_space,'SelectedObject'), 'Tag');
    0114
    0115 if (length(index_selected) == 1)
    0116
    0117         var1 = get_var_names(handles);
    0118         u = strfind(var1,'_');
    0119         cur_zone1 = var1(u(1)+1:u(2)-1);
    0120         cur_zone1 = strmatch(cur_zone1, handles.zone_names,'exact');
    0121         cur_seg1 = var1(u(2)+1:end);
    0122         cur_seg1 = strmatch(cur_seg1, handles.zone{cur_zone1}.seg_label,'exact');
    0123         cur_var1 = var1(1:strfind(var1,'_')-1);
    0124
    0125         if strcmp(phase_opt,'phase1D')
    0126             P.label = var1;
    0127             P.phase_opt = phase_opt;
    0128             [P.data P.bins] = eval(['ProbDist1D(handles.zone{' num2str(cur_zone1) '}.' cur_var1 ...
    0129                 '{' num2str(cur_seg1) '}.data ,' bins ')']);
    0130         elseif strcmp(phase_opt,'phase2D')
    0131             P.label = var1;
    0132             P.phase_opt = phase_opt;
    0133             [P.data P.bins] = eval(['ProbDist2D(handles.zone{' num2str(cur_zone1) '}.' cur_var1...
    0134                 '{' num2str(cur_seg1) '}.data ,' bins ')']);
    0135         end
    0136
    0137         %plot the distribution
    0138         figure
    0139         plot(P.bins,P.data)
    0140
    0141
    0142 elseif (length(index_selected) == 2)
    0143
    0144     % should only look at joint distribution for same speed segments in
    0145     % same zones right now.   The JointDist command expects equal length
    0146     % vectors.  The following variables are just calculated in case we
    0147     % modify JointDist down the road to handle vectors of different
    0148     % lengths.
    0149
    0150     if numel(str2num(bins)) == 2 | numel(str2num(bins))== 0
    0151         [var1 var2] = get_var_names(handles);
    0152         u1 = strfind(var1,'_');
    0153         u2 = strfind(var2,'_');
    0154         cur_zone1 = var1(u1(1)+1:u1(2)-1);
    0155         cur_zone1 = strmatch(cur_zone1, handles.zone_names,'exact');
    0156         cur_zone2 = var2(u2(1)+1:u2(2)-1);
    0157         cur_zone2 = strmatch(cur_zone2, handles.zone_names,'exact');
    0158         cur_seg1 = var1(u1(2)+1:end);
    0159         cur_seg1 = strmatch(cur_seg1, handles.zone{cur_zone1}.seg_label,'exact');
    0160         cur_seg2 = var2(u2(2)+1:end);
    0161         cur_seg2 = strmatch(cur_seg2, handles.zone{cur_zone2}.seg_label,'exact');
    0162         cur_var1 = var1(1:strfind(var1,'_')-1);
    0163         cur_var2 = var2(1:strfind(var2,'_')-1);
    0164
    0165         P.label = [var1 '_' var2];
    0166         P.phase_opt = 'N/A';
    0167         [P.data P.bins] = eval(['JointDist(handles.zone{' num2str(cur_zone1) '}.' cur_var1...
    0168                 '{' num2str(cur_seg1) '}.data , handles.zone{' num2str(cur_zone2) '}.' cur_var2...
    0169                 '{' num2str(cur_seg2) '}.data ,' bins ')']);
    0170
    0171         figure
    0172         imagesc(P.bins{2},P.bins{1},log(P.data))
    0173     else
    0174         errordlg('For a joint distribution, you must define bins or number of bins for BOTH directions!','Missing Bin Parameter')
    0175         return;
    0176     end
    0177 elseif length(index_selected) > 2
    0178         errordlg('You must select only one or two variables, no more than two!!!',...
    0179                 'Incorrect Selection','modal')
    0180 end
    0181
    0182 if exist('P')
    0183 handles.P{handles.called} = P;
    0184 else
    0185     handles.P{handles.called} = [];
    0186 end
    0187
    0188 guidata(gcbo,handles);
    0189
    0190
    0191 % --- Executes on button press in segment_speed.
    0192 function segment_speed_Callback(hObject, eventdata, handles)
    0193 % hObject    handle to segment_speed (see GCBO)
    0194 % eventdata  reserved - to be defined in a future version of MATLAB
    0195 % handles    structure with handles and user data (see GUIDATA)
    0196
    0197 noise_thresh = str2num(get(handles.noise_thresh,'String'));
    0198 NumZones = length(handles.zone_names);
    0199 list1 = {'x','y','r','theta','vx','vy','v','vtheta','tau','kappa','beta'};
    0200 fps = handles.fps;
    0201
    0202 disp('Segmenting speed.  Input parameters and wait...')
    0203 %based on how many spatial zones, ask how we want to segment speed.
    0204 %Remember that first zone is always the full arena.
    0205 n=0;
    0206 for i=1:NumZones
    0207     prompt1 = {['How many speed segments in ' handles.zone{i}.zone_label{1}]};
    0208     dlg_title1 = 'Segment Speed';
    0209     num_lines1 = 1;
    0210     def1 = {'2'};
    0211     num_segs = str2double(inputdlg(prompt1,dlg_title1,num_lines1,def1));
    0212     nameB4 = 'dummy';
    0213     seg_names = [];
    0214     speed_thresh = noise_thresh;
    0215     for j=1:num_segs-1;
    0216         prompt2 = {['Threshold between Segments ' num2str(j), ' and ' num2str(j+1) '.'],...
    0217             'Names for these zones:', ' '};
    0218         dlg_title2 = 'Please Input Speed Segmentation Data';
    0219         num_lines2 = 1;
    0220         def2 = {'0.75','NZS','FSS'};
    0221         answer = inputdlg(prompt2,dlg_title2,num_lines2,def2);
    0222         speed_thresh = [speed_thresh str2double(answer(1))];
    0223         nameA = answer(2);
    0224         nameB = answer(3);
    0225         check = strcmp(nameA,nameB4);
    0226         if (j~=1 & ~check)
    0227             errordlg([handles.zone{i}.zone_label{1} ' Segment ' num2str(j)...
    0228                 ' name must be same as previous ' handles.zone{i}.zone_label{1} ' Segment '...
    0229                  num2str(j) ' name! Rename!'])
    0230              return;
    0231         else
    0232             seg_names = [seg_names nameA nameB];
    0233         end
    0234         nameB4 = nameB;
    0235     end
    0236
    0237     if (num_segs == 1 | num_segs == 2)
    0238         seg_names = seg_names;
    0239     elseif (num_segs== 3)
    0240         seg_names = [seg_names(1:2) seg_names(end)];
    0241     else
    0242         seg_names = [seg_names(1:2) seg_names(end-1:end)];
    0243     end
    0244     %check speed thresholds in that zone
    0245     [s indx] = sort(speed_thresh,'ascend');
    0246     if (indx ~= [1:length(speed_thresh)])
    0247         errordlg(['Speed thresholds for successive segments should be larger than previous segments.  Try Again!'])
    0248              return;
    0249     elseif (~isempty(find(s < noise_thresh)))
    0250         errordlg(['None of the thresholds are permitted to be below the noise threshold. Try Again!'])
    0251              return;
    0252     end
    0253
    0254     seg_names = [{'all'} {'stops'} seg_names];
    0255
    0256
    0257         for q=1:length(seg_names)
    0258             for  j=1:length(list1)
    0259             if (strcmp(seg_names{q},'all') & strcmp(list1{j},'kappa')) | ...
    0260                     (strcmp(seg_names{q},'all') & strcmp(list1{j},'tau'))
    0261                temp_list{j,q+n*length(seg_names)} = [];
    0262             elseif (strcmp(seg_names{q},'stops') & strcmp(list1{j},'kappa')) | ...
    0263                     (strcmp(seg_names{q},'stops') & strcmp(list1{j},'beta'))
    0264                temp_list{j,q+n*length(seg_names)} = [];
    0265             elseif ~strcmp(seg_names{q},'all') & strcmp(list1{j},'beta')
    0266                     temp_list{j,q+n*length(seg_names)} = [];
    0267             else
    0268                 temp_list{j,q+n*length(seg_names)} = [list1{j} '_' handles.zone{i}.zone_label{1} '_' seg_names{q}];
    0269             end
    0270         end
    0271     end
    0272
    0273     %calculate stuff for this particular zone
    0274     t = handles.zone{i}.t{1}.data;
    0275     x = handles.zone{i}.x{1}.data;
    0276     y = handles.zone{i}.y{1}.data;
    0277     r = handles.zone{i}.r{1}.data;
    0278     theta = handles.zone{i}.theta{1}.data;
    0279     vx = handles.zone{i}.vx{1}.data;
    0280     vy = handles.zone{i}.vy{1}.data;
    0281     v = handles.zone{i}.v{1}.data;
    0282     vtheta =handles.zone{i}.vtheta{1}.data;
    0283
    0284
    0285     stops_indx = find(v < speed_thresh(1));
    0286     moves_indx = find(v >= speed_thresh(1));
    0287
    0288     v(stops_indx) = 0; vx(stops_indx) = 0; vy(stops_indx) = 0; vtheta = atan2(vy,vx);
    0289
    0290
    0291     handles.zone{i}.seg_label = seg_names;
    0292     handles.zone{i}.t{2}.data = t(stops_indx);
    0293     handles.zone{i}.x{2}.data = x(stops_indx);
    0294     handles.zone{i}.y{2}.data = y(stops_indx);
    0295     handles.zone{i}.r{2}.data = r(stops_indx);
    0296     handles.zone{i}.theta{2}.data = theta(stops_indx);
    0297     handles.zone{i}.vx{2}.data = vx(stops_indx);
    0298     handles.zone{i}.vy{2}.data = vy(stops_indx);
    0299     handles.zone{i}.v{2}.data = v(stops_indx);
    0300     handles.zone{i}.vtheta{2}.data = vtheta(stops_indx);
    0301     handles.zone{i}.tau{2}.data = FindDuration(stops_indx);
    0302     handles.zone{i}.kappa{2}.data = 'N/A';
    0303
    0304
    0305     for j=2:length(speed_thresh)
    0306
    0307         indxA =  find(v >= speed_thresh(j-1) & v < speed_thresh(j));
    0308         indxB =  find(v >= speed_thresh(j));
    0309
    0310         handles.zone{i}.t{j+1}.data = t(indxA);
    0311         handles.zone{i}.x{j+1}.data = x(indxA);
    0312         handles.zone{i}.y{j+1}.data = y(indxA);
    0313         handles.zone{i}.r{j+1}.data = r(indxA);
    0314         handles.zone{i}.theta{j+1}.data = theta(indxA);
    0315         handles.zone{i}.vx{j+1}.data = vx(indxA);
    0316         handles.zone{i}.vy{j+1}.data = vy(indxA);
    0317         handles.zone{i}.v{j+1}.data = v(indxA);
    0318         handles.zone{i}.vtheta{j+1}.data = vtheta(indxA);
    0319         handles.zone{i}.tau{j+1}.data = FindDuration(indxA);
    0320         handles.zone{i}.kappa{j+1}.data = CalcCurvature(v, vtheta, indxA, 1/fps);
    0321
    0322         handles.zone{i}.t{j+2}.data = t(indxB);
    0323         handles.zone{i}.x{j+2}.data = x(indxB);
    0324         handles.zone{i}.y{j+2}.data = y(indxB);
    0325         handles.zone{i}.r{j+2}.data = r(indxB);
    0326         handles.zone{i}.theta{j+2}.data = theta(indxB);
    0327         handles.zone{i}.vx{j+2}.data = vx(indxB);
    0328         handles.zone{i}.vy{j+2}.data = vy(indxB);
    0329         handles.zone{i}.v{j+2}.data = v(indxB);
    0330         handles.zone{i}.vtheta{j+2}.data = vtheta(indxB);
    0331         handles.zone{i}.tau{j+2}.data = FindDuration(indxB);
    0332         handles.zone{i}.kappa{j+2}.data = CalcCurvature(v, vtheta, indxB, 1/fps);
    0333
    0334     end
    0335     handles.zone{i}.beta{1}.data = CalcReorientAngle(vtheta, moves_indx);
    0336     n=n+1;
    0337 end
    0338
    0339
    0340 temp3 = {};
    0341 sz = size(temp_list);
    0342 for q=1:sz(2)
    0343     temp3 = [temp3 temp_list{:,q}];
    0344 end
    0345
    0346 disp('Speed has been segmented.')
    0347 update_listbox(handles, temp3)
    0348 guidata(gcbo,handles);
    0349
    0350 % --- Executes on button press in load_traj.
    0351 function load_traj_Callback(hObject, eventdata, handles)
    0352 % hObject    handle to load_traj (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 [filename, pathname] = uigetfile({'*.mat'}, 'Select the .mat file containing the trajectory', 'MultiSelect','off');
    0357
    0358 if isequal(filename,0) || isequal(pathname,0)
    0359     disp('File select canceled')
    0360     return;
    0361 else
    0362     disp(['File selected: ', fullfile(pathname, filename)])
    0363     load(fullfile(pathname,filename));
    0364     handles.filename = fullfile(pathname, filename);
    0365
    0366 end
    0367
    0368
    0369 handles.zone{1}.t{1}.data = t;
    0370 handles.fps = 1/(t(2)-t(1));
    0371 handles.zone{1}.x{1}.data = x;
    0372 handles.zone{1}.y{1}.data = y;
    0373
    0374 guidata(gcbo,handles);
    0375
    0376 % --- Executes on button press in smooth.
    0377 function smooth_Callback(hObject, eventdata, handles)
    0378 % hObject    handle to smooth (see GCBO)
    0379 % eventdata  reserved - to be defined in a future version of MATLAB
    0380 % handles    structure with handles and user data (see GUIDATA)
    0381
    0382 %grab data
    0383 t = handles.zone{1}.t{1}.data;
    0384 fps = handles.fps;
    0385 x = handles.zone{1}.x{1}.data;
    0386 y = handles.zone{1}.y{1}.data;
    0387 handles.P = [];
    0388
    0389 %grab smoothing parameters as input by the user
    0390 n = str2double(get(handles.n,'String'));
    0391 dn = str2double(get(handles.dn,'String'));
    0392
    0393 disp('Smoothing data.  Please wait...')
    0394 %smooth using runline
    0395 x = runline(x, n, dn);
    0396 y = runline(y, n, dn);
    0397
    0398 %Calculate polar coords and velocity.
    0399 r = sqrt(x.^2+y.^2);
    0400 theta = atan2(y,x);
    0401
    0402 vx = fps*gradient(x);
    0403 vy = fps*gradient(y);
    0404 v = sqrt(vx.^2+vy.^2);
    0405 vtheta = atan2(vy,vx);
    0406
    0407 clear handles.x;
    0408 clear handles.y;
    0409 %save smooth data to handles to be used by other GUI functions
    0410 handles.zone_names{1} = 'Full Arena';
    0411 handles.zone{1}.zone_label = {'Full Arena'};
    0412 handles.zone{1}.seg_label = {'all'};
    0413 handles.zone{1}.t{1}.data = t;
    0414 handles.zone{1}.x{1}.data = x;
    0415 handles.zone{1}.y{1}.data = y;
    0416
    0417 handles.zone{1}.r{1}.data = r;
    0418 handles.zone{1}.theta{1}.data = theta;
    0419 handles.zone{1}.vx{1}.data = vx;
    0420 handles.zone{1}.vy{1}.data = vy;
    0421 handles.zone{1}.v{1}.data = v;
    0422 handles.zone{1}.vtheta{1}.data = vtheta;
    0423 handles.zone{1}.tau{1}.data = 'N/A';
    0424 handles.zone{1}.kappa{1}.data = 'N/A';
    0425
    0426
    0427
    0428 disp('Trajectory has been smoothed.')
    0429
    0430 update_listbox(handles, {'x_Full Arena_all','y_Full Arena_all','r_Full Arena_all','theta_Full Arena_all',...
    0431     'vx_Full Arena_all','vy_Full Arena_all','v_Full Arena_all','vtheta_Full Arena_all'});
    0432 guidata(gcbo,handles);
    0433
    0434 function n_Callback(hObject, eventdata, handles)
    0435 % hObject    handle to n (see GCBO)
    0436 % eventdata  reserved - to be defined in a future version of MATLAB
    0437 % handles    structure with handles and user data (see GUIDATA)
    0438
    0439 % Hints: get(hObject,'String') returns contents of n as text
    0440 %        str2double(get(hObject,'String')) returns contents of n as a double
    0441
    0442
    0443 % --- Executes during object creation, after setting all properties.
    0444 function n_CreateFcn(hObject, eventdata, handles)
    0445 % hObject    handle to n (see GCBO)
    0446 % eventdata  reserved - to be defined in a future version of MATLAB
    0447 % handles    empty - handles not created until after all CreateFcns called
    0448
    0449 % Hint: edit controls usually have a white background on Windows.
    0450 %       See ISPC and COMPUTER.
    0451 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0452     set(hObject,'BackgroundColor','white');
    0453 end
    0454
    0455
    0456 function dn_Callback(hObject, eventdata, handles)
    0457 % hObject    handle to dn (see GCBO)
    0458 % eventdata  reserved - to be defined in a future version of MATLAB
    0459 % handles    structure with handles and user data (see GUIDATA)
    0460
    0461 % Hints: get(hObject,'String') returns contents of dn as text
    0462 %        str2double(get(hObject,'String')) returns contents of dn as a double
    0463
    0464
    0465 % --- Executes during object creation, after setting all properties.
    0466 function dn_CreateFcn(hObject, eventdata, handles)
    0467 % hObject    handle to dn (see GCBO)
    0468 % eventdata  reserved - to be defined in a future version of MATLAB
    0469 % handles    empty - handles not created until after all CreateFcns called
    0470
    0471 % Hint: edit controls usually have a white background on Windows.
    0472 %       See ISPC and COMPUTER.
    0473 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0474     set(hObject,'BackgroundColor','white');
    0475 end
    0476
    0477
    0478 % --- Executes on button press in view_traj.
    0479 function view_traj_Callback(hObject, eventdata, handles)
    0480 % hObject    handle to view_traj (see GCBO)
    0481 % eventdata  reserved - to be defined in a future version of MATLAB
    0482 % handles    structure with handles and user data (see GUIDATA)
    0483
    0484 t = handles.zone{1}.t{1}.data;
    0485 x = handles.zone{1}.x{1}.data;
    0486 y = handles.zone{1}.y{1}.data;
    0487 tmp = getfield(handles, 'zone');
    0488
    0489 if (isfield(tmp{1},'r'))
    0490     r = handles.zone{1}.r{1}.data;
    0491     theta = handles.zone{1}.theta{1}.data;
    0492     vx = handles.zone{1}.vx{1}.data;
    0493     vy = handles.zone{1}.vy{1}.data;
    0494     v = handles.zone{1}.v{1}.data;
    0495     vtheta =handles.zone{1}.vtheta{1}.data;
    0496 end
    0497
    0498 str = get(handles.variables, 'String');
    0499 val = get(handles.variables, 'Value');
    0500
    0501 % Set current data to the selected data set.
    0502       if strcmp(str(val),'(x,y)')
    0503           figure
    0504           plot(x,y)
    0505           xlabel('x-position')
    0506           ylabel('y-position')
    0507           title('Trajectory')
    0508           if (~isfield(tmp{1},'r'))
    0509               disp('You can view your trajectory, but you MUST smooth your data before proceeding with any calculations!')
    0510           end
    0511       else
    0512
    0513           var1 = 't';
    0514           if exist(str{val})
    0515               figure
    0516               var2 = str{val};
    0517               eval(['plot(' var1 ','  var2 ')'])
    0518               xlabel('Time')
    0519               ylabel(var2)
    0520               xlim([0 t(end)])
    0521           else
    0522               errordlg('Can"t view this variable unless data is smoothed','Non-existant Variable')
    0523
    0524               return;
    0525           end
    0526       end
    0527
    0528 % --- Executes on selection change in variables.
    0529 function variables_Callback(hObject, eventdata, handles)
    0530 % hObject    handle to variables (see GCBO)
    0531 % eventdata  reserved - to be defined in a future version of MATLAB
    0532 % handles    structure with handles and user data (see GUIDATA)
    0533
    0534 % Hints: contents = get(hObject,'String') returns variables contents as cell array
    0535 %        contents{get(hObject,'Value')} returns selected item from variables
    0536
    0537 set(hObject,'String',{'(x,y)','x','y','r','theta','vx','vy','v','vtheta'})
    0538
    0539 guidata(gcbo,handles);
    0540
    0541 % --- Executes during object creation, after setting all properties.
    0542 function variables_CreateFcn(hObject, eventdata, handles)
    0543 % hObject    handle to variables (see GCBO)
    0544 % eventdata  reserved - to be defined in a future version of MATLAB
    0545 % handles    empty - handles not created until after all CreateFcns called
    0546
    0547 % Hint: popupmenu controls usually have a white background on Windows.
    0548 %       See ISPC and COMPUTER.
    0549 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0550     set(hObject,'BackgroundColor','white');
    0551 end
    0552
    0553 set(hObject,'String',{'(x,y)','x','y','r','theta','vx','vy','v','vtheta'})
    0554
    0555 guidata(gcbo,handles);
    0556 % --- Executes on button press in segment_space.
    0557 function segment_space_Callback(hObject, eventdata, handles)
    0558 % hObject    handle to segment_space (see GCBO)
    0559 % eventdata  reserved - to be defined in a future version of MATLAB
    0560 % handles    structure with handles and user data (see GUIDATA)
    0561
    0562 NumZones = str2double(get(handles.num_zones,'String'));
    0563 if NumZones < 2
    0564     errordlg('Must have at least 2 zones','Too few zones')
    0565     return;
    0566 end
    0567
    0568 zone_indx = 1:NumZones;
    0569 list1 = {'x','y','r','theta','vx','vy','v','vtheta','tau','kappa'};
    0570 zone_names = handles.zone_names;
    0571 handles.zone_names = {};
    0572 handles.zone_names{1} = 'Full Arena';
    0573
    0574 t = handles.zone{1}.t{1}.data;
    0575 dt = t(2)-t(1);
    0576 x = handles.zone{1}.x{1}.data;
    0577 y = handles.zone{1}.y{1}.data;
    0578 r = handles.zone{1}.r{1}.data;
    0579 theta = handles.zone{1}.theta{1}.data;
    0580 vx = handles.zone{1}.vx{1}.data;
    0581 vy = handles.zone{1}.vy{1}.data;
    0582 v = handles.zone{1}.v{1}.data;
    0583 vtheta =handles.zone{1}.vtheta{1}.data;
    0584
    0585 disp('Segmenting Space.  Input parameters and wait...')
    0586
    0587 thresh(1) = 0;
    0588 for i=1:NumZones-1
    0589     prompt = {['Threshold between Zones ' num2str(zone_indx(i)), ' and ' num2str(zone_indx(i+1)) '.'],...
    0590         'Names for these zones:', ' '};
    0591     dlg_title = 'Please Input Spatial Zone Data';
    0592     num_lines = 1;
    0593     def = {'7.3','CZ','RZ'};
    0594     answer{i} = inputdlg(prompt,dlg_title,num_lines,def);
    0595     thresh(i+1) = str2double(answer{i}(1));
    0596 end
    0597
    0598 zone_names = [zone_names answer{1}(2)];
    0599 for i=1:NumZones-2
    0600     nameA = answer{i}(2);
    0601     nameB = answer{i}(3);
    0602     nameC = answer{i+1}(2);
    0603     if (~strcmp(nameB,nameC))
    0604         errordlg(['Zone ' num2str(i+1) ' name must be same as previous Zone ' num2str(i+1) ' name! Rename!'])
    0605     else
    0606         zone_names = [zone_names nameB];
    0607     end
    0608 end
    0609 zone_names = [zone_names answer{NumZones-1}(3)];
    0610
    0611 for i=1:length(zone_names)
    0612     for  j=1:length(list1)
    0613         if  strcmp(zone_names{i},'Full Arena') & (strcmp(list1{j},'kappa') | strcmp(list1{j},'tau'))
    0614             temp_list{j,i} = [];
    0615         elseif strcmp(list1{j},'kappa')
    0616             temp_list{j,i} = [];
    0617         else
    0618         temp_list{j,i} = [list1{j} '_' zone_names{i} '_all'];
    0619         end
    0620     end
    0621 end
    0622
    0623 temp3 = {};
    0624 for i=1:NumZones+1
    0625     temp3 = [temp3 temp_list{:,i}];
    0626 end
    0627
    0628 handles.zone_names = zone_names;
    0629
    0630 %Now using thresholds, divvy up space
    0631 for i = 2:length(thresh)
    0632
    0633         % Can only do radial zones in this version of FAnalyze
    0634         indxA =  find(r >= thresh(i-1) & r < thresh(i));
    0635         indxB =  find(r >= thresh(i));
    0636
    0637         handles.zone{i}.zone_label = zone_names(i);
    0638         handles.zone{i}.seg_label = {'all'};
    0639         handles.zone{i}.t{1}.data = t(indxA);
    0640         handles.zone{i}.x{1}.data = x(indxA);
    0641         handles.zone{i}.y{1}.data = y(indxA);
    0642         handles.zone{i}.r{1}.data = r(indxA);
    0643         handles.zone{i}.theta{1}.data = theta(indxA);
    0644         handles.zone{i}.vx{1}.data = vx(indxA);
    0645         handles.zone{i}.vy{1}.data = vy(indxA);
    0646         handles.zone{i}.v{1}.data = v(indxA);
    0647         handles.zone{i}.vtheta{1}.data = vtheta(indxA);
    0648         handles.zone{i}.tau{1}.data = FindDuration(indxA);
    0649         handles.zone{i}.kappa{1}.data = CalcCurvature(v, vtheta, indxA, dt);
    0650
    0651         handles.zone{i+1}.zone_label = zone_names(i+1);
    0652         handles.zone{i+1}.seg_label = {'all'};
    0653         handles.zone{i+1}.t{1}.data = t(indxB);
    0654         handles.zone{i+1}.x{1}.data = x(indxB);
    0655         handles.zone{i+1}.y{1}.data = y(indxB);
    0656         handles.zone{i+1}.r{1}.data = r(indxB);
    0657         handles.zone{i+1}.theta{1}.data = theta(indxB);
    0658         handles.zone{i+1}.vx{1}.data = vx(indxB);
    0659         handles.zone{i+1}.vy{1}.data = vy(indxB);
    0660         handles.zone{i+1}.v{1}.data = v(indxB);
    0661         handles.zone{i+1}.vtheta{1}.data = vtheta(indxB);
    0662
    0663
    0664         handles.zone{i+1}.tau{1}.data = FindDuration(indxB);
    0665         handles.zone{i+1}.kappa{1}.data = CalcCurvature(v, vtheta, indxB, dt);
    0666
    0667
    0668
    0669 end
    0670
    0671 update_listbox(handles, temp3)
    0672 disp('Space has been segmented.')
    0673 guidata(gcbo,handles);
    0674
    0675 % --- Executes on button press in save_ws.
    0676 function save_ws_Callback(hObject, eventdata, handles)
    0677 % hObject    handle to save_ws (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 % %This whole function just defines variables and saves the workspace as a
    0682 % %.mat file.
    0683 [filename, pathname] = uiputfile('*.mat', 'Pick a MAT file to save to');
    0684
    0685 traj = handles.zone;
    0686 P = handles.P;
    0687 fps = handles.fps;
    0688 save(fullfile(pathname, filename),'traj','P')
    0689 disp(['Data has been saved to ' fullfile(pathname, filename)])
    0690
    0691 function update_listbox(handles, vars)
    0692 % this function updates the message center at the bottom of the GUI
    0693 % adapted from Mike Rieser's PControl GUI.
    0694
    0695 set(handles.ws_vars, 'String', vars);
    0696
    0697 %%%%%%%%%%%%%%%%
    0698 function varargout = get_var_names(handles)
    0699 list_entries = get(handles.ws_vars,'String');
    0700 index_selected = get(handles.ws_vars,'Value');
    0701
    0702 varargout = list_entries(index_selected);
    0703
    0704
    0705
    0706
    0707
    0708 function num_zones_Callback(hObject, eventdata, handles)
    0709 % hObject    handle to num_zones (see GCBO)
    0710 % eventdata  reserved - to be defined in a future version of MATLAB
    0711 % handles    structure with handles and user data (see GUIDATA)
    0712
    0713 % Hints: get(hObject,'String') returns contents of num_zones as text
    0714 %        str2double(get(hObject,'String')) returns contents of num_zones as a double
    0715
    0716
    0717 % --- Executes during object creation, after setting all properties.
    0718 function num_zones_CreateFcn(hObject, eventdata, handles)
    0719 % hObject    handle to num_zones (see GCBO)
    0720 % eventdata  reserved - to be defined in a future version of MATLAB
    0721 % handles    empty - handles not created until after all CreateFcns called
    0722
    0723 % Hint: edit controls usually have a white background on Windows.
    0724 %       See ISPC and COMPUTER.
    0725 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0726     set(hObject,'BackgroundColor','white');
    0727 end
    0728
    0729
    0730
    0731 function noise_thresh_Callback(hObject, eventdata, handles)
    0732 % hObject    handle to noise_thresh (see GCBO)
    0733 % eventdata  reserved - to be defined in a future version of MATLAB
    0734 % handles    structure with handles and user data (see GUIDATA)
    0735
    0736 % Hints: get(hObject,'String') returns contents of noise_thresh as text
    0737 %        str2double(get(hObject,'String')) returns contents of noise_thresh as a double
    0738
    0739
    0740
    0741
    0742 % --- Executes during object creation, after setting all properties.
    0743 function noise_thresh_CreateFcn(hObject, eventdata, handles)
    0744 % hObject    handle to noise_thresh (see GCBO)
    0745 % eventdata  reserved - to be defined in a future version of MATLAB
    0746 % handles    empty - handles not created until after all CreateFcns called
    0747
    0748 % Hint: edit controls usually have a white background on Windows.
    0749 %       See ISPC and COMPUTER.
    0750 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    0751     set(hObject,'BackgroundColor','white');
    0752 end
    0753
    0754