function[mat trial_ind] = getTrialMatrix(obj, varargin) %% getTrialMatrix returns traces of all selected trials in selection, surviving the Quality Threshold % % Usage % [mat trial_ind] = PDSession.getTrialMatrix([selection, eye, signal_type, corrected]) % % - mat % Matrix of n x l, n = number of trials, l = maximum length of all traces % - trial_ind % Vector containing the selected trials after quality % thresholding % - selection % Vector of selected / requested trials % - eye % 'left', 'right', or 'both' (default = 'left') % - signal_type % 'raw', 'interpolated', 'filtered' (default = 'filtered'); % - corrected % 0 /1, use baseline corrected signals (default : 1) % % Examples: % % *) without parameters : session.getTrialMatrix; % *) with one parameter : session.getTrialMatrix(1:10); % *) with two parameters : session.getTrialMatrix('all', 'left'); % *) with three parameters : session.getTrialMatrix('all', 'left', 'interpolated'); % *) with four parameters : session.getTrialMatrix(1:10, 'right', 'interpolated', 1); if nargin == 2 selection = varargin{1}; eye = 'left'; signal_type = 'filtered'; corrected = 1; end if nargin == 3 selection = varargin{1}; eye = varargin{2}; signal_type = 'filtered'; corrected = 1; end if nargin == 4 selection = varargin{1}; eye = varargin{2}; signal_type = varargin{3}; corrected = 1; end if corrected == 1 data_field = 'baseline_corrected'; else data_field = 'raw'; end if ~ismember(eye,{'left', 'right', 'both'}); eye = 'left'; end if isempty(selection) selection = 1:length(obj.trials); end %% Quality check q_objs = [obj.trials(selection).quality]; q_vals = cat(1, q_objs.(eye)); valid_trials = q_vals >= obj.settings.QualityThreshold; trial_ind = find(valid_trials); trials = [obj.trials(valid_trials).data]; t_objs = [trials.(eye)]; data_objs = [t_objs.(data_field)]; max_trial_length = 0; for i = 1:length(data_objs) if max_trial_length < length(data_objs(i).(signal_type)); max_trial_length = length(data_objs(i).(signal_type)); end end %% define empty output matrix with zeros mat = nan(length(valid_trials), max_trial_length); for t = 1:length(trials) trace = trials(t).(eye).(data_field).(signal_type); mat(t,1:length(trace)) = trace; end