Home > database > extract_formdata.m

extract_formdata

PURPOSE ^

Extracts data for given forms in the given response table.

SYNOPSIS ^

function [data] = extract_formdata(resp_tbl,form_ids,varargin)

DESCRIPTION ^

 Extracts data for given forms in the given response table.
 [data] = extract_formdata(resp_tbl, form_ids, varargin);

 Generic data extraction for data associated with a specfic list of forms in an
 experiment response table in the database. Data is a cell array with each
 form in one element

 A flexible number of options can be passed into the function as tag/value
 pairs.

 'conn_id', conn_id - the ID of the mysql connection - REQUIRED
 'subids', {subids} - a cell array of subject IDs
 'sessids', [sessids] - a vector of session IDs
 'extract_vars', {extract_vars} - cell array of field names to
                                  extract. Default is all.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data] = extract_formdata(resp_tbl,form_ids,varargin)
0002 % Extracts data for given forms in the given response table.
0003 % [data] = extract_formdata(resp_tbl, form_ids, varargin);
0004 %
0005 % Generic data extraction for data associated with a specfic list of forms in an
0006 % experiment response table in the database. Data is a cell array with each
0007 % form in one element
0008 %
0009 % A flexible number of options can be passed into the function as tag/value
0010 % pairs.
0011 %
0012 % 'conn_id', conn_id - the ID of the mysql connection - REQUIRED
0013 % 'subids', {subids} - a cell array of subject IDs
0014 % 'sessids', [sessids] - a vector of session IDs
0015 % 'extract_vars', {extract_vars} - cell array of field names to
0016 %                                  extract. Default is all.
0017 
0018 % 06/22/06 PJ - Adapted from extract_generic
0019 % 10/28/06 PJ - enabled handling of vector of form_ids.
0020 % 06/15/10 PJ - sanitized mysql_make_conn
0021 
0022 % Parse the input arguments
0023 narg = length(varargin);
0024 for iarg = 1:2:narg
0025   switch varargin{iarg}
0026     case 'conn_id'
0027       conn_id = varargin{iarg+1};
0028     case 'subids'
0029       subids = varargin{iarg+1};
0030     case 'sessids'
0031       sessids = varargin{iarg+1};
0032     case 'extract_vars'
0033       extract_vars = varargin{iarg+1};
0034     otherwise
0035       fprintf('extract_formdata: Unknown input argument: %s\n', varargin{iarg});
0036   end
0037 end
0038 
0039 if ~exist('conn_id','var') || isempty(conn_id)
0040   error('%s: Do not have a valid connection ID', mfilename);
0041 end
0042 
0043 try 
0044   extract_vars{1}; 
0045 catch
0046   % This should be made more dynamic by getting a list of all field names in
0047   % the desired response table.
0048   extract_vars = {...
0049     'subject_id', ...
0050     'session_id', ...
0051     'stimulus_id', ...
0052     'date_time', ...
0053     'question_id', ...
0054     'subquestion', ...
0055     'question_iteration', ...
0056     'response_id', ...
0057     'response_enum', ...
0058     'response_text' ...
0059     };
0060 end
0061     
0062 try subids(1);
0063   subject_str = sprintf('"%s",', subids{:});
0064   subject_str(end) = [];
0065   subject_str = sprintf('AND subject_id IN (%s)', subject_str);
0066 catch
0067   subject_str = 'AND 1';
0068 end
0069   
0070 try sessids(1);
0071   sess_str = sprintf('%d,', sessids);
0072   sess_str(end) = [];
0073   sess_str = sprintf('AND session_id IN (%s)', sess_str);
0074 catch
0075   sess_str = 'AND 1';
0076 end
0077 
0078 nform = length(form_ids);
0079 data = cell(nform,1);
0080 for iform = 1:nform
0081   data{iform}.vars = extract_vars;
0082   form_id = form_ids(iform);
0083   % Extract the data
0084   sql_str = sprintf('SELECT %s FROM %s WHERE form_id=%d %s %s;', cell2str(extract_vars,','), resp_tbl, form_id, subject_str, sess_str);
0085   [tmp{1:length(extract_vars)}] = mysql(conn_id,sql_str);
0086   data{iform}.data = tmp;
0087 
0088   % Resolve the question IDs to get the associated question text
0089   sql_str = sprintf(['SELECT question_text FROM question,%s WHERE' ...
0090     ' question.question_id=%s.question_id AND %s.form_id=%d ' ...
0091     ' %s %s;'], resp_tbl, resp_tbl, resp_tbl, form_id, subject_str, sess_str);
0092   [qtxt] = mysql(conn_id,sql_str);
0093   data{iform}.vars{end+1} = 'question_text';
0094   data{iform}.data{end+1} = qtxt;
0095 
0096   % Get the subquestion text
0097   sql_str = sprintf(['SELECT heading FROM' ...
0098     ' question_x_data_format,%s WHERE' ...
0099     ' question_x_data_format.question_id=%s.question_id AND' ...
0100     ' question_x_data_format.subquestion=%s.subquestion AND %s.form_id=%d %s %s;'], ...
0101       resp_tbl, resp_tbl, resp_tbl, resp_tbl, form_id, subject_str, sess_str);
0102   [heading] = mysql(conn_id,sql_str);
0103   data{iform}.vars{end+1} = 'heading';
0104   data{iform}.data{end+1} = heading;
0105 end % for iform
0106 
0107 if ~conn_id
0108   mysql(conn_id,'close');
0109 end
0110 
0111 return

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003