0001 function [data] = extract_formdata(resp_tbl,form_ids,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
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
0047
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
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
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
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
0106
0107 if ~conn_id
0108 mysql(conn_id,'close');
0109 end
0110
0111 return