Displays written responses associated text or varchar fields. outdata = ensemble_report_written(data_st,params); Displays written responses associated text or varchar fields. Currently, the reporting is rather primitive, but should be enhanced to support various tabular formats. Table format could be specified by providing a vector of compqids to appear in the columns. A structure array could actually provide qid info as well as formatting info, e.g. column width, etc. Note: Currently, the script will not treat the same question appearing on different forms as a different instance of the question. If you don't want answers to the same question on different forms combined, you must filter the data to only process forms with unique question IDs. This behavior may change in future versions. params.ensemble.conn_id - mysql connection to use params.filt - any filtering criteria that are to be applied params.report - structure containing directives regarding the output. See ensemble_init_fid() params.report.print - generate output params.report.write2file - write output to a file (otherwise stdout) params.report.fname - name of file to write responses to params.report.filemode - defaults to 'wt' params.report.verbose - defaults to 1
0001 function an_st = ensemble_report_written(data_st,params) 0002 % Displays written responses associated text or varchar fields. 0003 % 0004 % outdata = ensemble_report_written(data_st,params); 0005 % 0006 % Displays written responses associated text or varchar fields. Currently, the 0007 % reporting is rather primitive, but should be enhanced to support various 0008 % tabular formats. Table format could be specified by providing a vector of 0009 % compqids to appear in the columns. A structure array could actually provide 0010 % qid info as well as formatting info, e.g. column width, etc. 0011 % 0012 % Note: Currently, the script will not treat the same question appearing on 0013 % different forms as a different instance of the question. If you don't want 0014 % answers to the same question on different forms combined, you must filter the 0015 % data to only process forms with unique question IDs. This behavior may 0016 % change in future versions. 0017 % 0018 % params.ensemble.conn_id - mysql connection to use 0019 % params.filt - any filtering criteria that are to be applied 0020 % params.report - structure containing directives regarding the output. 0021 % See ensemble_init_fid() 0022 % params.report.print - generate output 0023 % params.report.write2file - write output to a file (otherwise stdout) 0024 % params.report.fname - name of file to write responses to 0025 % params.report.filemode - defaults to 'wt' 0026 % params.report.verbose - defaults to 1 0027 0028 % 04/29/07 Petr Janata - adapted from ensemble_enum_stats 0029 % 07/17/08 PJ - fixed call to ensemble_init_fid to conform to new param handling 0030 % 23Nov2011 PJ - improved handling of conn_id 0031 0032 try 0033 if isfield(params, 'mysql') 0034 conn_id = params.mysql.conn_id; 0035 elseif isfield(params, 'ensemble') 0036 conn_id = params.ensemble.conn_id; 0037 end 0038 catch ME 0039 conn_id = []; 0040 end 0041 0042 an_st = ensemble_init_data_struct; 0043 an_st.type = 'enum_written_by_compqid'; 0044 0045 % Make sure we have a compqid variable 0046 data_st = ensemble_check_compqid(data_st); 0047 if isempty(data_st) 0048 return 0049 end 0050 0051 % Set the column constants 0052 incol = set_var_col_const(data_st.vars); 0053 0054 % Apply any specified filtering to the input data 0055 if isfield(params,'filt') 0056 fprintf('Applying filtering criteria\n') 0057 data_st = ensemble_filter(data_st, params.filt); 0058 end 0059 0060 % 0061 % Gather metadata on the questions 0062 % 0063 0064 % Get a list of unique composite question IDs 0065 fprintf('Getting list of unique composite question IDs\n') 0066 %qids = fix(unique(data_st.data{incol.compqid})); 0067 qids = unique(fix(data_st.data{incol.compqid})); 0068 0069 fprintf('Extracting question metadata\n'); 0070 qinfo = mysql_extract_metadata('table','question', ... 0071 'question_id',qids, ... 0072 'conn_id', conn_id); 0073 0074 % Figure out which of the questions in the qinfo structure are varchar or text and remove 0075 % those that are not 0076 fprintf('Removing non-text questions\n'); 0077 qinfo_char_mask = ismember({qinfo.type},{'varchar','text'}); 0078 if sum(~qinfo_char_mask) 0079 qinfo(~qinfo_char_mask) = []; 0080 end 0081 0082 % Create masks for all of the response data 0083 char_compqids = [qinfo.compqid]; 0084 0085 % Filter the data again 0086 filt.include.any.compqid = char_compqids; 0087 data_st = ensemble_filter(data_st,filt); 0088 0089 % Precalculate the subject masks 0090 %[sub_mask_mtx, subids] = make_mask_mtx(data_st.data{incol.subject_id}); 0091 %nsub = length(subids); 0092 0093 % 0094 % Check to see if we are writing all responses to a single file and open a file 0095 % if necessary 0096 % 0097 if ~isfield(params,'report') 0098 params.report = struct(); 0099 end 0100 composite_fid = ensemble_init_fid(params.report); 0101 0102 % See if we are only writing responses to the file and not question or subject 0103 % information 0104 try response_only = params.report.response_only; catch response_only = false; end 0105 0106 % 0107 % Loop over all of the unique question/subquestion combinations or compqids 0108 % 0109 nqid = length(qinfo); 0110 for iqid = 1:nqid 0111 curr_id = qinfo(iqid).compqid; 0112 an_st.vars{iqid} = sprintf('compqid %s',num2str(curr_id)); 0113 0114 % Copy the question info over to the display parameter structure in case we 0115 % are going to display some of the data 0116 params.display.qinfo = qinfo(iqid); 0117 0118 % Filter the data based on the current ID 0119 clear qfilt 0120 qfilt.include.all.compqid = curr_id; 0121 curr_st = ensemble_filter(data_st,qfilt); 0122 col = set_var_col_const(curr_st.vars); 0123 0124 if ~response_only 0125 fprintf(composite_fid,'\n\nCompqid: %.2f\nQUESTION: %s\nSUBQUESTION: %s\n', ... 0126 curr_id, qinfo(iqid).question_text, qinfo(iqid).heading); 0127 end 0128 0129 % Loop over the rows in the data and write out each response to the file 0130 nrows = length(curr_st.data{col.subject_id}); 0131 for irow = 1:nrows 0132 if ~response_only 0133 subject_str = sprintf('%s (%d): ', ... 0134 curr_st.data{col.subject_id}{irow}, ... 0135 curr_st.data{col.session_id}(irow)); 0136 else 0137 subject_str = ''; 0138 end 0139 fprintf(composite_fid,'\n%s%s\n', subject_str, curr_st.data{col.response_text}{irow}); 0140 end 0141 end % for iqid 0142 0143 if composite_fid > 1 0144 fclose(composite_fid); 0145 end 0146 0147 an_st.meta.params = params; 0148 0149 end % function ensemble_report_written 0150