Home > data-access > get_variable_by_trial.m

get_variable_by_trial

PURPOSE ^

GET_VARIABLE_BY_TRIAL Get only the valid portion of a variable's data

SYNOPSIS ^

function [ chunks ] = get_variable_by_trial( subject_list, var_name, trial_values)

DESCRIPTION ^

GET_VARIABLE_BY_TRIAL Get only the valid portion of a variable's data
   USAGE:
   get_variable_by_trial(SUBJECT_LIST, VAR_NAME, TRIAL_VALUES)
       For each subject ID in SUBJECT_LIST, retrieves the variable
       specified by VAR_NAME, as well as the trial information for that
       subject.  For each trial, finds the data in the variable that falls
       within that interval, and puts it in one cell of a cell array.
       Returns a cell array, with one trial of data per cell.
 
       Sometimes there were multiple trials for one subject, by specifying
       the value of TRIAL_VALUES, user can only output the selected trials
       for each subject, instead of all trials. The input value can be a 
       single vector (the same value for all subject), or a cell of 
       vectors (one vector per each subject).

   SUBJECT_LIST is just an array of subject IDs.  See also: LIST_SUBJECTS,
   FIND_SUBJECTS

   VAR_NAME is the name of a variable.  It can be any of the standard
   multi-lib variable types.

   The return value is a cell array with one trial's worth of data per
   cell.  If no data exists in a trial, for instance if it's an
   invalid trial marked by start and end times of -1, some cells may
   contain an empty array---but it might not be equal to [], use isempty()
   to test for this.

   See also: EXTRACT_RANGES, CELLFUN

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ chunks ] = get_variable_by_trial( subject_list, var_name, trial_values)
0002 %GET_VARIABLE_BY_TRIAL Get only the valid portion of a variable's data
0003 %   USAGE:
0004 %   get_variable_by_trial(SUBJECT_LIST, VAR_NAME, TRIAL_VALUES)
0005 %       For each subject ID in SUBJECT_LIST, retrieves the variable
0006 %       specified by VAR_NAME, as well as the trial information for that
0007 %       subject.  For each trial, finds the data in the variable that falls
0008 %       within that interval, and puts it in one cell of a cell array.
0009 %       Returns a cell array, with one trial of data per cell.
0010 %
0011 %       Sometimes there were multiple trials for one subject, by specifying
0012 %       the value of TRIAL_VALUES, user can only output the selected trials
0013 %       for each subject, instead of all trials. The input value can be a
0014 %       single vector (the same value for all subject), or a cell of
0015 %       vectors (one vector per each subject).
0016 %
0017 %   SUBJECT_LIST is just an array of subject IDs.  See also: LIST_SUBJECTS,
0018 %   FIND_SUBJECTS
0019 %
0020 %   VAR_NAME is the name of a variable.  It can be any of the standard
0021 %   multi-lib variable types.
0022 %
0023 %   The return value is a cell array with one trial's worth of data per
0024 %   cell.  If no data exists in a trial, for instance if it's an
0025 %   invalid trial marked by start and end times of -1, some cells may
0026 %   contain an empty array---but it might not be equal to [], use isempty()
0027 %   to test for this.
0028 %
0029 %   See also: EXTRACT_RANGES, CELLFUN
0030 %
0031 
0032 data_by_subject = get_variable_by_subject(subject_list, var_name);
0033 
0034 if nargin == 2
0035     trials_by_subject = arrayfun(@(subj_id) get_trial_times(subj_id), ...
0036         subject_list, ...
0037         'UniformOutput', 0)';
0038 else    
0039     if iscell(trial_values) && length(subject_list) ~= length(trial_values)
0040         error(['Length of argument ''trial_values'' should be either' ...
0041             ' one (same trial values for each subject), or the same ' ...
0042             'length as ''subject_list'' (one set per subject).']);
0043     end
0044     
0045     if iscell(trial_values)
0046         subject_list = num2cell(subject_list);
0047         
0048         trials_by_subject = cellfun( ...
0049         @(sub_id, t_values) get_trial_times(sub_id, t_values), ...
0050         subject_list, trial_values, ...
0051         'UniformOutput', 0)';
0052     else
0053         trials_by_subject = arrayfun(@(subj_id) ...
0054             get_trial_times(subj_id, trial_values), ...
0055             subject_list, ...
0056             'UniformOutput', 0)';
0057     end
0058 end        
0059 
0060 data_type = get_data_type(var_name);
0061 
0062 chunked_data_by_subject = cellfun( ...
0063     @(data, trials) extract_ranges(data, data_type, trials), ...
0064     data_by_subject, ...
0065     trials_by_subject, ...
0066     'UniformOutput', 0);
0067 
0068 chunks = vertcat(chunked_data_by_subject{:});
0069 if isempty(chunks)
0070     % vertcat returns a scalar array if its input is empty
0071     chunks = {};
0072 end
0073 
0074 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005