0001 function ensemble_print_metadata(data_str,params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 DEFAULT_EXPERIMENT_META = {'experiment_id','experiment_title','response_table'};
0034 DEFAULT_FORM_META = {'form_id','form_name','header'};
0035 DEFAULT_QUESTION_META = {'question_id','subquestion','question_text','answer_format'};
0036
0037
0038 try write2file = params.report.tables.write2file; catch write2file = 0, end
0039
0040 if write2file
0041 try fid = params.report.tables.fid;
0042 catch
0043 fid = fopen(params.report.tables.fname,'wt');
0044 params.report.tables.fid = fid;
0045 if fid == -1
0046 error(sprintf('Problem opening logfile: %s\n',params.report.tables.fname))
0047 end
0048 fprintf('Writing tables to file: %s\n', ...
0049 params.report.tables.fname);
0050 closeFid = 1;
0051 end
0052 else
0053 fid = 1;
0054 params.report.tables.fid = fid;
0055 end
0056
0057
0058 if(~isfield(params.report.tables,'columns') | isempty(params.report.tables.columns))
0059 if(isfield(data_str(1),'experiment_id'))
0060 params.report.tables.columns = {DEFAULT_EXPERIMENT_META,DEFAULT_FORM_META, DEFAULT_QUESTION_META};
0061 elseif(isfield(data_str(1),'form_id'))
0062 params.report.tables.columns = {DEFAULT_FORM_META, DEFAULT_QUESTION_META};
0063 elseif(isfield(data_str(1),'question_id'))
0064 params.report.tables.columns = DEFAULT_QUESTION_META;
0065 end
0066 end
0067
0068
0069
0070
0071 if(isfield(data_str(1),'experiment_id'))
0072 nForms = length(data_str.form);
0073 fprintf(fid,'EXPERIMENT TITLE "%s"\n',data_str.experiment_title);
0074 fprintf(fid,'%d FORMS\n\n',nForms);
0075
0076 ensemble_print_table(data_str,params);
0077
0078
0079 childParams = params;
0080 childParams.report.tables.columns = params.report.tables.columns(2:end);
0081
0082 for iForm = 1:nForms
0083 ensemble_print_metadata(data_str.form(iForm),childParams);
0084 end
0085
0086 elseif(isfield(data_str(1),'form_id'))
0087
0088 nQuestions = length(data_str.question);
0089 if(nQuestions == 1)
0090 qString = 'question';
0091 else
0092 qString = 'questions';
0093 end
0094
0095 fprintf(fid,'\nForm Name "%s"\n',data_str.form_name);
0096 fprintf(fid,'%d %s\n\n',nQuestions,qString);
0097 ensemble_print_table(data_str,params);
0098
0099 if(nQuestions > 0)
0100 fprintf(fid,'\n');
0101 childParams = params;
0102 childParams.report.tables.columns = params.report.tables.columns(2:end);
0103 ensemble_print_metadata(data_str.question,childParams);
0104 end
0105
0106 elseif(isfield(data_str(1),'question_id'))
0107 ensemble_print_table(data_str,params);
0108 end
0109
0110 if(write2file & exist('closeFid','var'))
0111 fclose(fid);
0112 end
0113
0114 return
0115
0116
0117
0118
0119 function ensemble_print_table(data_st,params)
0120
0121
0122 fid = params.report.tables.fid;
0123
0124
0125
0126
0127
0128
0129 LF_ASCII = 10;
0130 CR_ASCII = 13;
0131 TAB_ASCII = 9;
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 if(iscell(params.report.tables.columns{1}))
0144 displayColumns = params.report.tables.columns{1};
0145 else
0146 displayColumns = params.report.tables.columns;
0147 end
0148
0149
0150
0151
0152
0153 [answerFormatIsListed,answerFormatLoc] = ismember('answer_format',displayColumns);
0154 if(answerFormatIsListed)
0155 displayColumns{answerFormatLoc} = 'enum_values';
0156 end
0157
0158 numColumns = length(displayColumns);
0159 numRecords = length(data_st);
0160
0161
0162
0163
0164
0165 headingReformat = { 'experiment_id','Experiment ID';
0166 'start_date','Start Date';
0167 'experiment_title','Experiment Title';
0168 'experiment_description','Experiment Description';
0169 'response_table','Response Table';
0170 'irb_id','IRB ID';
0171 'end_date','End Date';
0172 'language','Language';
0173 'locked','Locked';
0174 'form_id','Form ID';
0175 'form_name','Form Name';
0176 'form_category','Form Category';
0177 'header','Header';
0178 'footer','Footer';
0179 'version','Version';
0180 'locked','Locked';
0181 'condition','Condition';
0182 'condition_matlab','Condition Matlab';
0183 'visit_once','Visit Once';
0184 'compqid','Composite Question ID';
0185 'question_id','Question ID';
0186 'question_text','Question Text';
0187 'question_category','Question Category';
0188 'heading_format','Heading Format';
0189 'locked','Locked';
0190 'data_format_id','Data Format ID';
0191 'type','Type';
0192 'enum_values','Answer Format';
0193 'subquestion','Subquestion';
0194 'heading','Heading';
0195 'range','Range';
0196 'default','Default';
0197 'html_field_type','HTML Field Type';
0198 'required','Required'};
0199
0200
0201 [tf,colRftIdx] = ismember(displayColumns,{headingReformat{:,1}});
0202
0203 for iColumn = 1:numColumns
0204 columnName = displayColumns{iColumn};
0205 columnHeading = headingReformat{colRftIdx(iColumn),2};
0206 fprintf(fid,'%s',columnHeading);
0207
0208 if(iColumn ~= numColumns)
0209 fprintf(fid,'\t');
0210 end
0211 end
0212
0213 fprintf(fid,'\n');
0214
0215 for iRecord = 1:numRecords
0216 for iColumn = 1:numColumns
0217
0218
0219
0220 if(~isfield(data_st(iRecord),displayColumns{iColumn}))
0221 error(sprintf('Field %s is invalid for specified table structure', displayColumns{iColumn}));
0222 end
0223
0224
0225
0226 fieldVal = getfield(data_st(iRecord),displayColumns{iColumn});
0227
0228
0229 if (ischar(fieldVal) | iscell(fieldVal))
0230 fieldVal = strrep(fieldVal,'\r','');
0231 fieldVal = strrep(fieldVal,'\t','');
0232 fieldVal = strrep(fieldVal,'\n','');
0233 end
0234
0235
0236 if(isnumeric(fieldVal))
0237
0238 spacer = [];
0239 fmtString = ['%' num2str(spacer) 'd'];
0240 fprintf(fid,fmtString,fieldVal);
0241
0242
0243 elseif(strcmp(displayColumns{iColumn},'enum_values'))
0244
0245
0246 if(~isempty(fieldVal))
0247 for iCell = 1:length(fieldVal)
0248 if(iCell == 1)
0249
0250 spacer = [];
0251 fmtString = ['%' num2str(spacer) 'd=%s,'];
0252 elseif(iCell == length(fieldVal))
0253 fmtString = '%d=%s';
0254 else
0255 fmtString = '%d=%s, ';
0256 end
0257
0258 fprintf(fid,fmtString,iCell,fieldVal{iCell});
0259 end
0260
0261 else
0262
0263 dataType = getfield(data_st(iRecord),'type');
0264 switch dataType
0265 case {'int16','int32','int64'}
0266 fieldReport = 'Any Integer';
0267 case 'double'
0268 fieldReport = 'Any Double';
0269 case {'varchar','text'}
0270 fieldReport = 'Any String';
0271 case 'date'
0272 fieldReport = 'Any valid date';
0273 case 'year'
0274 fieldReport = 'Any valid year';
0275 case 'null'
0276 fieldReport = 'None';
0277 end
0278
0279
0280 spacer = [];
0281 fmtString = ['%' num2str(spacer) 's\t'];
0282
0283 fprintf(fid,fmtString,fieldReport);
0284
0285 end
0286
0287 elseif(ischar(fieldVal))
0288
0289
0290
0291 fieldVal(find(fieldVal == CR_ASCII | fieldVal == LF_ASCII | fieldVal == TAB_ASCII)) = [];
0292
0293
0294 spacer = [];
0295 fmtString = ['%' num2str(spacer) 's'];
0296 fprintf(fid,fmtString,fieldVal);
0297
0298
0299 end
0300
0301 if(iColumn ~= numColumns)
0302 fprintf(fid,'\t');
0303 end
0304
0305 end
0306
0307 fprintf(fid,'\n');
0308 end
0309
0310
0311
0312
0313 return