PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/afni_matlab/GroupAna.m

http://princeton-mvpa-toolbox.googlecode.com/
MATLAB | 1626 lines | 1320 code | 156 blank | 150 comment | 302 complexity | a58eb921517b55dae3e16f2c52466fc6 MD5 | raw file
Possible License(s): GPL-3.0, BSD-2-Clause, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. %GroupAna.m
  2. %
  3. %Purpose:
  4. %
  5. %
  6. %
  7. %Input Parameters:
  8. %
  9. %
  10. %
  11. %Output Parameters:
  12. % err : 0 No Problem
  13. % : 1 Problems
  14. %
  15. %
  16. %
  17. %Key Terms:
  18. %
  19. %More Info :
  20. %
  21. %
  22. %
  23. diary('diary'); % save all subsequent command window input and most of the resulting command window output to be appended
  24. % to file 'diary'
  25. fprintf('\nWelcome to GroupAna, AFNI Group Analysis Package!');
  26. fprintf('\n-----------------------------------------------------------\n');
  27. fprintf('\nVersion 1.0.1, Nov. 23, 2005');
  28. fprintf('\nAuthor: Gang Chen');
  29. fprintf('\nSSCC/NIMH/ National Institutes of Health, Bethesda MD 20892');
  30. fprintf('\n-----------------------------------------------------------\n');
  31. %Define the function name for easy referencing
  32. FuncName = 'GroupAna.m';
  33. %Debug Flag
  34. DBG = 1;
  35. %initailize return variables
  36. err = 1;
  37. clear all;
  38. fprintf('\nPlease read the following carefully about group analysis setup:\n');
  39. fprintf('\n\t1. If the resolution of your EPI data is near n millimeters, during Talairach conversion use\n');
  40. fprintf('\t "the command adwarp -dxyz n" to greatly reduce runtime without sacrificing accuracy.\n');
  41. fprintf('\n\t2. We strongly suggest that factor names be labeled with short (2 or 3 capital letters) names\n');
  42. fprintf('\t so that subbrik labels can be shown on AFNI viewer.\n');
  43. fprintf('\n\t3. With nesting, arrange your design in such a way that the last factor is nested within the 1st factor.\n');
  44. %fprintf('\n\t4. The program can only handle balanced design now. \n');
  45. fprintf('\n\t4. Each input file should include only one subbrik. We suggest files be \n');
  46. fprintf('\t named by reflecting the hierarchy of the experiment design.\n');
  47. fprintf('\n\t5. Currently all of the following terms are modeled: main effects and applicable interactions in various orders, .\n');
  48. %fprintf('\n\t6. One covariate is currently allowed in the analysis, which should be in the format of one-column text file.\n');
  49. %fprintf('\t The column length has to be the same as the total number of input files.\n');
  50. % Grouop analysis for Volume or Surface data?
  51. flg = 0;
  52. while flg == 0,
  53. data_type = input('\nGroups analysis for volume or surface data (0 - volume; 1 - surface)? ');
  54. if (data_type ~= 0 & data_type ~= 1),
  55. flg = 0; fprintf(2,'Error: wrong input! Please try it again.\n');
  56. else flg = 1;
  57. end
  58. end
  59. % If for surface data, acquire number of nodes
  60. flg = 0;
  61. if (data_type == 1),
  62. fprintf(1, '\nInput files have to be in 1D format, and program @Purify_1D can be used to extract each regressor');
  63. fprintf(1, '\ncoefficient column to a 1D file. Type @Purify_1D for usage ...\n');
  64. Frame_N = input('\nWhich column corresponds to regressor coefficient in the 1D files? (1, 2, 3, ...) ');
  65. while flg == 0,
  66. node_n = input('\nHow many number of nodes in the surface data? ');
  67. if (isnumeric(node_n) == 0 | isempty(node_n)),
  68. flg = 0; fprintf(2,'Error: the input is not a number. Please try it again.\n');
  69. else flg = 1; end
  70. end
  71. else Opt.Frames = 1; % In the case of volumetric data, it's supposed to have only ONE subbrik!
  72. end
  73. flg = 0;
  74. while flg == 0,
  75. NF = input('\nHow many factors? ');
  76. if (isnumeric(NF) == 0 | isempty(NF)),
  77. flg = 0; fprintf(2,'Error: the input is not a number. Please try it again.\n');
  78. else flg = 1;
  79. end
  80. end
  81. switch NF
  82. case 1,
  83. fprintf('\nAvailable design types: \n');
  84. fprintf('\n\tType 1: one factor with fixed effect.\n');
  85. case 2,
  86. fprintf('\nAvailabe design types:\n');
  87. fprintf('\n\tType 1: Factorial (crossed) design AXB - both factors are fixed.\n');
  88. fprintf('\n\tType 2: Factorial (crossed) design AXB - factor A is fixed while B is random. If B is subject, it is');
  89. fprintf('\n\t usually called 1-way design with A varying within subject. Notice: It is inappropriate to');
  90. fprintf('\n\t run any constrasts including mean estimates and differences for factor B with this design type.\n');
  91. fprintf('\n\tType 3: Factorial (crossed) design AXB - both factors are random.\n');
  92. case 3,
  93. fprintf('\nAvailabe design types:\n');
  94. fprintf('\n\tType 1: Factorial (crossed) design AXBXC - all factors are fixed.\n');
  95. fprintf('\n\tType 2: Factorial (crossed) design AXBXC - factors A and B are fixed while C is random. If C is subject, it is');
  96. fprintf('\n\t usually called 2-way design with A and B varying within subject. Notice: It is inappropriate to');
  97. fprintf('\n\t run any constrast tests including mean estimates and differences for factor C with this design type.\n');
  98. fprintf('\n\tType 3: Mixed design BXC(A) - A and B are fixed while C (usually subject) is random and nested within A.\n');
  99. fprintf('\n\tType 4: Mixed design BXC(A) - Fixed factor C is nested within fixed factor A while B (usually subject) is random.\n');
  100. case 4,
  101. fprintf('\nAvailabe design types:\n');
  102. fprintf('\n\tType 1: Factorial (crossed) design AXBXCXD - all 4 factors are fixed.\n');
  103. fprintf('\n\tType 2: Factorial (crossed) design AXBXCXD - only factor D is random. If D is subject it is also');
  104. fprintf('\n\t called 3-way design with all 3 factors A, B, and C varying within subject.');
  105. fprintf('\n\tType 3: Mixed design BXCXD(A)- only the nested (4th) factor D (usually subject) is random.');
  106. fprintf('\n\t Also called 3-way design with factors B and C varying within subject and factor A between subjects.');
  107. fprintf('\n\tType 4: Mixed design BXCXD(A)- D is nested within A, but only the 3rd factor (usually subject)');
  108. fprintf('\n\t is random.');
  109. fprintf('\n\tType 5: Mixed design CXD(AXB) - only the nested (4th) factor D (usually subject) is random,');
  110. fprintf('\n\t but factor D is nested within both factors A and B. If D is subject it is also called 3-way');
  111. fprintf('\n\t design with factor C varying within-subject and factors A and B between-subjects.\n');
  112. % fprintf('\nNotice: This is NOT an exhaustive list of design types for 4-way ANOVA. Other types might be implemented upon request.\n');
  113. case 5,
  114. fprintf('\nAvailabe design types:\n');
  115. fprintf('\n\tType 1: Factorial (crossed) design AXBXCXDXE - all 5 factors are fixed.\n');
  116. fprintf('\n\tType 2: Factorial (crossed) design AXBXCXDXE - only factor E is random. If E is subject it is also');
  117. fprintf('\n\t called 4-way design with all 4 factors A, B, C and D varying within subject.\n');
  118. fprintf('\n\tType 3: Mixed design BXCXDXE(A) - only the nested (5th) factor E (usually subject) is random.');
  119. fprintf('\n\t Also called 4-way design with factors B, C and D varying within subject and factor A between subjects.\n');
  120. fprintf('\n\tType 4: Mixed design BXCXDXE(A) - the 5th factor E is nested within factor A, but factor D (usually subject)');
  121. fprintf('\n\t is random.\n');
  122. end
  123. flg = 0;
  124. while flg == 0,
  125. dsgn = input('\nChoose design type (1, 2, 3, 4, 5): ');
  126. if (isnumeric(dsgn) == 0 | isempty(dsgn)),
  127. flg = 0; fprintf(2,'Error: input is not a number. Please try it again.\n');
  128. else flg = 1;
  129. end
  130. end
  131. %flg = 0;
  132. %while flg == 0,
  133. % slices = input('How many slices along the Z axis (run 3dinfo on one of the input files to find out)? ');
  134. % if (isnumeric(slices) == 0 | isempty(slices)),
  135. % flg = 0; fprintf(2,'Error: the input is not a number. Please try it again.\n');
  136. % else flg = 1;
  137. % end
  138. %end
  139. % Balanced design?
  140. flg = 0;
  141. while flg == 0,
  142. unbalanced.yes = 1 - input('\nIs the design balanced? (1 - Yes; 0 - No) ');
  143. if (unbalanced.yes ~= 0 & unbalanced.yes ~= 1),
  144. flg = 0; fprintf(2,'Error: inapproriate input. Please try it again.\n');
  145. else flg = 1;
  146. end
  147. if (unbalanced.yes == 1),
  148. fprintf('\nThe following two kinds of unbalanced designs are currently allowed:\n')
  149. fprintf('\n(1) All factors are fixed - ');
  150. fprintf('\n\t 1-way ANOVA; 2-way ANOVA type 1: AXB; and 3-way ANOVA type 1: AXBXC');
  151. fprintf('\n\n(2) When a random factor (subject) is nested within another factor A,');
  152. fprintf('\n\t each level of factor A contains a unique and unequal number of subjects - ');
  153. fprintf('\n\t 3-way ANOVA type 3: BXC(A); 4-way ANOVA types 3: BXCXD(A); ');
  154. fprintf('\n\t and 4-way ANOVA type 5: CXD(AXB).')
  155. if (input('\n\nDoes your unbalanced design belong to either of the above types? (1 - Yes; 0 - No) ') == 0);
  156. while (1); fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  157. end
  158. end
  159. % fprintf('\nTwo kinds of unbalanced design are considered:\n');
  160. % fprintf('\nType 1: When subject is a random factor and nested within another factor (3-way ANOVA type 3, 4-way ANOVA types 3 and 5),');
  161. % fprintf('\n\t there can be different number of subjects for each level of factor A;\n');
  162. % fprintf('\nType 2: Not type 1, but there are different sample sizes for some cells (factor level combinations).');
  163. % fprintf('\n\t For example, add somethint here later...\n');
  164. %
  165. % unbalanced.type = input('\nChoose design type (1, 2): ');
  166. % if (unbalanced.type ~= 1 & unbalanced.type ~= 2),
  167. % flg = 0; fprintf(2,'Error: inapproriate input. Please try it again.\n');
  168. % else flg = 1;
  169. % end
  170. end
  171. for (ii = 1:1:5)
  172. FL(ii).N_level = 0; % initialization for ContrVec.m so that their values are available for lower-way ANOVA
  173. end
  174. %Get the number of levels for each factor and also the total number of input files
  175. flg = 0;
  176. ntot = 1;
  177. if (unbalanced.yes == 1),
  178. if ((NF == 1 | NF == 2 | NF == 3 | NF == 4) & dsgn == 1), % Basically for 1,2,3,4-way ANCOVA: This loop is the same as balanced. Maybe also for 4-way ANCOVA?
  179. for (i=1:1:NF),
  180. fprintf('\nLabel for No. %i ', i);
  181. FL(i).expr = input('factor: ', 's'); % Factor Label i
  182. fprintf(2,'How many levels does factor %c (%s) ', 64+i, FL(i).expr);
  183. FL(i).N_level = input('have? ');
  184. if (isnumeric(FL(i).N_level) == 0 | isempty(FL(i).N_level)),
  185. flg = 0; fprintf(2,'Error: the input is not a number. Please try again.\n');
  186. else flg = 1; end
  187. for (j=1:1:FL(i).N_level),
  188. fprintf('Label for No. %i level of factor %c (%s)', j, 64+i, FL(i).expr);
  189. FL(i).level(j).expr = input(' is: ', 's');
  190. end
  191. sz(i) = FL(i).N_level; % number of levels of factor i
  192. ntot = ntot * FL(i).N_level; %total number of combinations
  193. end
  194. end
  195. if (((NF == 3 | NF == 4) & dsgn == 3)),
  196. % if (unbalanced.type == 1),
  197. for (i=1:1:(NF-1)),
  198. fprintf('\nLabel for No. %i ', i);
  199. FL(i).expr = input('factor: ', 's'); % Factor Label i
  200. fprintf(2,'How many levels does factor %c (%s) ', 64+i, FL(i).expr);
  201. FL(i).N_level = input('have? ');
  202. if (isnumeric(FL(i).N_level) == 0 | isempty(FL(i).N_level)),
  203. flg = 0; fprintf(2,'Error: the input is not a number. Please try again.\n');
  204. else flg = 1;
  205. end
  206. for (j=1:1:FL(i).N_level),
  207. fprintf('Label for No. %i level of factor %c (%s)', j, 64+i, FL(i).expr);
  208. FL(i).level(j).expr = input(' is: ', 's');
  209. end
  210. sz(i) = FL(i).N_level; % number of levels of factor i
  211. ntot = ntot * FL(i).N_level; %total number of combinations
  212. end % for (i=1:1:(NF-1))
  213. FL(NF).N_level = 0;
  214. fprintf(2, '\nLabel for No. %i ', NF);
  215. FL(NF).expr = input('factor: ', 's'); % Label this unbalanced factor
  216. % fprintf(2, '\nNote: Since this is a nested design the label for levels (usuall subject names) of factor No. %i (%c - %s)', NF, 64+NF, FL(NF).expr);
  217. % fprintf(2, '\nhas to be DIFFERENT for each level of factor %c (%s)!!!\n\n', 64+1, FL(1).expr);
  218. flag = 0;
  219. while flag == 0,
  220. combine = [];
  221. for (i = 1:1:FL(1).N_level),
  222. fprintf(2,'How many levels does factor %c (%s) corresponding to level %i (%s) of factor %c (%s) ', ...
  223. 64+NF, FL(NF).expr, i, FL(1).level(i).expr, 64+1, FL(1).expr);
  224. FL(NF).UL(i).N_level = input('have? '); % unbalanced levels for this factor
  225. for (j=1:1:FL(NF).UL(i).N_level),
  226. fprintf('Label for No. %i level of factor %c (%s) in group %i of factor %c (%s)', j, 64+NF, FL(NF).expr, i, 64+1, FL(1).expr);
  227. FL(NF).UL(i).n(j).expr = input(' is: ', 's');
  228. combine = [combine {FL(NF).UL(i).n(j).expr}]; % Concatenate them to make a cell array
  229. end
  230. % FL(NF).N_level = FL(NF).N_level + FL(NF).UL(i).N_level;
  231. % FL(NF).N_level = max([FL(NF).UL(:).N_level]); % This is for positioning those contrast columns in the design matrix in PreProc.m
  232. end
  233. if (length(unique(combine)) == max([FL(NF).UL(:).N_level])), % if same labels are used across groups
  234. FL(NF).N_level = max([FL(NF).UL(:).N_level]); flag = 1; % design matrix is built based on the longest group length
  235. ntot = ntot * sum([FL(NF).UL(:).N_level]) / FL(1).N_level;
  236. elseif (length(unique(combine)) == sum([FL(NF).UL(:).N_level])), % if different labels are used across groups
  237. FL(NF).N_level = sum([FL(NF).UL(:).N_level]); flag = 1; % design matrix is built based on the total length of the groups
  238. ntot = ntot * FL(NF).N_level / FL(1).N_level;
  239. else
  240. flag = 0;
  241. fprintf(2, '\nError: There is some overlap among the labels of factor %c (%s) across the groups of factor %c (%s)!\n', ...
  242. 64+NF, FL(NF).expr, 64+1, FL(1).expr);
  243. end
  244. end % while flag == 0
  245. end % if (((NF == 3 | NF == 4) & dsgn == 3))
  246. if (NF == 4 & dsgn == 5), % CXD(AXB)
  247. for (i=1:1:(NF-1)), % Get information for factors A, B, and C
  248. fprintf('\nLabel for No. %i ', i);
  249. FL(i).expr = input('factor: ', 's'); % Factor Label i
  250. fprintf(2,'How many levels does factor %c (%s) ', 64+i, FL(i).expr);
  251. FL(i).N_level = input('have? ');
  252. if (isnumeric(FL(i).N_level) == 0 | isempty(FL(i).N_level)),
  253. flg = 0; fprintf(2,'Error: the input is not a number. Please try again.\n');
  254. else flg = 1;
  255. end
  256. for (j=1:1:FL(i).N_level),
  257. fprintf('Label for No. %i level of factor %c (%s)', j, 64+i, FL(i).expr);
  258. FL(i).level(j).expr = input(' is: ', 's');
  259. end
  260. sz(i) = FL(i).N_level; % number of levels of factor i
  261. ntot = ntot * FL(i).N_level; %total number of combinations
  262. end % for (i=1:1:(NF-1))
  263. FL(NF).N_level = 0; % Info for factor D, which is nested with both A and B
  264. fprintf(2, '\nLabel for No. %i ', NF);
  265. FL(NF).expr = input('factor: ', 's'); % Label this unbalanced factor
  266. flag = 0;
  267. while flag == 0,
  268. combine = [];
  269. for (ii = 1:1:FL(1).N_level), % factor A
  270. for (jj = 1:1:FL(2).N_level), % factor B
  271. fprintf(2,'How many levels does factor %c (%s) corresponding to level %i (%s) of factor %c (%s) and level %i (%s) of factor %c (%s) ', ...
  272. 64+NF, FL(NF).expr, ii, FL(1).level(ii).expr, 64+1, FL(1).expr, jj, FL(2).level(jj).expr, 64+2, FL(2).expr);
  273. FL(NF).UL(ii, jj).N_level = input('have? '); % unbalanced levels for this factor
  274. for (kk=1:1:FL(NF).UL(ii, jj).N_level),
  275. fprintf('Label for No. %i level of factor %c (%s) in group %i of factor %c (%s) and group %i of factor %c (%s)', ...
  276. kk, 64+NF, FL(NF).expr, ii, 64+1, FL(1).expr, jj, 64+2, FL(2).expr);
  277. FL(NF).UL(ii, jj).n(kk).expr = input(' is: ', 's');
  278. combine = [combine {FL(NF).UL(ii, jj).n(kk).expr}]; % Concatenate them to make a cell array
  279. end
  280. end % jj
  281. end % ii
  282. if (length(unique(combine)) == max([FL(NF).UL.N_level])), % if same labels are used across groups
  283. FL(NF).N_level = max([FL(NF).UL.N_level]); flag = 1; % design matrix is built based on the longest group length
  284. ntot = ntot * sum([FL(NF).UL.N_level]) / (FL(1).N_level * FL(2).N_level); % total number of input files
  285. % elseif (length(unique(combine)) == sum([FL(NF).UL.N_level])), % if different labels are used across groups
  286. % FL(NF).N_level = sum([FL(NF).UL.N_level]); flag = 1; % design matrix is built based on the total length of the groups
  287. % ntot = ntot * FL(NF).N_level / (FL(1).N_level * FL(2).N_level);
  288. % else
  289. % flag = 0;
  290. % fprintf(2, '\nError: There is some overlap among the labels of factor %c (%s) across the groups of factor %c (%s) and factor %c (%s)!\n', ...
  291. % 64+NF, FL(NF).expr, 64+1, FL(1).expr, 64+2, FL(2).expr);
  292. else
  293. flag = 0;
  294. fprintf(2, '\nError: Currently the labels for different groups have to be the same for this design.');
  295. end
  296. end % while flag == 0
  297. end % (NF == 4 & dsgn == 5)
  298. else % Balanced designs
  299. for (i=1:1:NF),
  300. fprintf('\nLabel for No. %i ', i);
  301. FL(i).expr = input('factor: ', 's'); % Factor Label i
  302. fprintf(2,'How many levels does factor %c (%s) ', 64+i, FL(i).expr);
  303. FL(i).N_level = input('have? ');
  304. if (isnumeric(FL(i).N_level) == 0 | isempty(FL(i).N_level)),
  305. flg = 0; fprintf(2,'Error: the input is not a number. Please try again.\n');
  306. else flg = 1;
  307. end
  308. for (j=1:1:FL(i).N_level),
  309. fprintf('Label for No. %i level of factor %c (%s)', j, 64+i, FL(i).expr);
  310. FL(i).level(j).expr = input(' is: ', 's');
  311. end
  312. sz(i) = FL(i).N_level; % number of levels of factor i
  313. ntot = ntot * FL(i).N_level; %total number of combinations
  314. end
  315. end % if (unbalanced)
  316. if ~((NF == 1 | NF == 2 | NF == 3 | NF == 4) & dsgn == 1 & unbalanced.yes == 1),
  317. fprintf(2,'\nEnter the sample size (number of observations) per combination');
  318. FL(NF+1).N_level = input(': '); % Should it be changed to a different name instead of FL???
  319. ntot = ntot * FL(NF+1).N_level; % total number of factor combinations including repeats
  320. sz(NF+1) = FL(NF+1).N_level;
  321. end
  322. % Running ANCOVA?
  323. flg = 0;
  324. %cov.label = [];
  325. %while flg == 0,
  326. % cov.do = input('\nAny covariate (concomitant variable)? (1 - Yes; 0 - No) ');
  327. % if (cov.do ~= 0 & cov.do ~= 1),
  328. % flg = 0; fprintf(2,'Error: inapproriate input. Please try it again.\n');
  329. % else flg = 1;
  330. % end
  331. %end
  332. cov.do = 0;
  333. %Only allow one covariate now!
  334. if (cov.do),
  335. FL(NF+1).N_level = 1; % This is for PreProc.m to get the correct 'shift' position
  336. if (NF == 2 & dsgn == 3), % 2-way ANOVA of A(R)XB(R) possible in FMRI?
  337. fprintf('\nThe 2-way ANCOVA for this design is currently NOT available.\n');
  338. while (1); fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  339. end
  340. flg = 0;
  341. cov.label = input('Label for the covariate is: ', 's');
  342. fprintf('The 1D file for the covariate has to be in the format of one-column text.\n');
  343. fprintf('And the column should have exactly the same number and order of those input files.\n');
  344. while flg == 0,
  345. cov.FN = input('\nConvariate file name: ', 's');
  346. fid0 = fopen(cov.FN,'r');
  347. if (fid0 == -1),
  348. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', cov.FN);
  349. else flg = 1;
  350. [cov.vec, count] = fscanf(fid0, '%f');
  351. if (count ~= ntot & ~((NF ==1 | NF == 2 | NF == 3) & dsgn == 1)), % Check length of the 1D file
  352. fprintf(2, '\nError: The column length of the covariate has to equal to the total number of input files!\n');
  353. fprintf(2,'Halted: Ctrl+c to exit'); pause;
  354. end
  355. cov.vec = cov.vec - mean(cov.vec);
  356. end
  357. end % while
  358. end
  359. %flg = 0;
  360. %while flg == 0,
  361. % fprintf('\nIs input in the format of one-subbrik-one-file or one-file-per-subject?\n');
  362. % file_format = input('(1 - Single brik; 0 - Multiple subbriks) ');
  363. % if (file_format ~= 0 & file_format ~= 1),
  364. % flg = 0; fprintf(2,'Error: inapproriate input. Please try it again.\n');
  365. % else flg = 1;
  366. % end
  367. %end
  368. fprintf('\nAll input files are supposed to contain only one subbrik.\n');
  369. file_format = 1; % Ignore that file_format = 0 now since it leads to too much trouble
  370. if (file_format == 0), % unused now!
  371. flg = 0;
  372. file_num = input('Number of input files: ');
  373. file_SB = input('Number of subbriks for analysis in each file: ');
  374. if (isnumeric(file_num) == 0 | isempty(file_num) | isnumeric(file_SB) == 0 | isempty(file_SB)),
  375. flg = 0; fprintf(2,'Error: the input is not a number. Please try it again.\n');
  376. else flg = 1; end
  377. if (file_num*file_SB ~= ntot),
  378. fprintf(2, '\nError: The number of files and subbriks are not consistent!\n');
  379. fprintf(2,'Halted: Ctrl+c to exit'); pause;
  380. end
  381. end % if (file_format == 0)
  382. if (file_format == 1 & ~((NF == 1 | NF == 2 | NF == 3 | NF == 4) & dsgn == 1)),
  383. fprintf(2,'\nThere should be totally %i input files. \n', ntot);
  384. corr = input('Correct? (1 - Yes; 0 - No) ');
  385. if (corr == 0),
  386. fprintf(2,'Error: Check the inconsistency. \n');
  387. while (1); fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  388. end
  389. end
  390. %generate the subcripts and obtain all input files
  391. GP = cell(NF, ntot); %creat a cell array to reflect the structure of all the combinations
  392. if (file_format == 1),
  393. if (unbalanced.yes == 1), % Try 4-way design 3 first
  394. % if ((NF == 1 | NF == 2 | NF == 3 | NF == 4) & dsgn == 1), % Meant for 1,2,3,4-way ANCOVA with unequal sample size
  395. if (dsgn == 1),
  396. FI = 0; % File index
  397. flg = 0;
  398. ntot = input('Total number of input files: ');
  399. if (isnumeric(ntot) == 0 | isempty(ntot)),
  400. flg = 0; fprintf(2,'Error: the input is not a number. Please try again.\n');
  401. else flg = 1;
  402. end
  403. switch NF
  404. case 1,
  405. for (ii = 1:1:FL(1).N_level),
  406. fprintf (2,'\nFor factor %c (%s) at level %i (%s),', 64+1, FL(1).expr, ii, FL(1).level(ii).expr);
  407. sz = input('\nsample size is: ');
  408. fprintf (2,'\nProvide those %i input files:\n', sz);
  409. for (rr = 1:1:sz),
  410. FI = FI + 1;
  411. GP(1, FI) = {FL(1).level(ii).expr};
  412. flg = 0;
  413. while flg == 0,
  414. fprintf (2,'No. %i file ', rr);
  415. file(FI).nm = input('is: ', 's');
  416. fid = fopen (file(FI).nm,'r');
  417. if (fid == -1), flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  418. else flg = 1; fclose (fid); end
  419. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  420. format = 'tlrc';
  421. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  422. format = 'orig';
  423. else
  424. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  425. format = '1D';
  426. else
  427. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  428. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  429. end
  430. end
  431. end
  432. end % for (rr = 1:1:sz)
  433. end % for (ii = 1:1:FL(1).N_level)
  434. case 2,
  435. for (ii = 1:1:FL(1).N_level),
  436. for (jj = 1:1:FL(2).N_level),
  437. fprintf (2,'\nFor factor %c (%s) at level %i (%s),', 64+1, FL(1).expr, ii, FL(1).level(ii).expr);
  438. fprintf (2,'\n factor %c (%s) at level %i (%s),', 64+2, FL(2).expr, jj, FL(2).level(jj).expr);
  439. sz = input('\nsample size is: ');
  440. fprintf (2,'\nProvide those %i input files:\n', sz);
  441. for (rr = 1:1:sz),
  442. FI = FI + 1;
  443. GP(1, FI) = {FL(1).level(ii).expr};
  444. GP(2, FI) = {FL(2).level(jj).expr};
  445. flg = 0;
  446. while flg == 0,
  447. fprintf (2,'No. %i file ', rr);
  448. file(FI).nm = input('is: ', 's');
  449. fid = fopen (file(FI).nm,'r');
  450. if (fid == -1), flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  451. else flg = 1; fclose (fid); end
  452. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  453. format = 'tlrc';
  454. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  455. format = 'orig';
  456. else
  457. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  458. format = '1D';
  459. else
  460. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  461. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  462. end
  463. end
  464. end
  465. end % for (rr = 1:1:sz)
  466. end % for (jj = 1:1:FL(2).N_level)
  467. end % for (ii = 1:1:FL(1).N_level)
  468. case 3,
  469. for (ii = 1:1:FL(1).N_level),
  470. for (jj = 1:1:FL(2).N_level),
  471. for (kk = 1:1:FL(3).N_level),
  472. fprintf (2,'\nFor factor %c (%s) at level %i (%s),', 64+1, FL(1).expr, ii, FL(1).level(ii).expr);
  473. fprintf (2,'\n factor %c (%s) at level %i (%s),', 64+2, FL(2).expr, jj, FL(2).level(jj).expr);
  474. fprintf (2,'\n factor %c (%s) at level %i (%s),', 64+3, FL(3).expr, kk, FL(3).level(kk).expr);
  475. sz = input('\nsample size is: ');
  476. fprintf (2,'\nProvide those %i input files:\n', sz);
  477. for (rr = 1:1:sz),
  478. FI = FI + 1;
  479. GP(1, FI) = {FL(1).level(ii).expr};
  480. GP(2, FI) = {FL(2).level(jj).expr};
  481. GP(3, FI) = {FL(3).level(kk).expr};
  482. flg = 0;
  483. while flg == 0,
  484. fprintf (2,'No. %i file ', rr);
  485. file(FI).nm = input('is: ', 's');
  486. fid = fopen (file(FI).nm,'r');
  487. if (fid == -1), flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  488. else flg = 1; fclose (fid); end
  489. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  490. format = 'tlrc';
  491. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  492. format = 'orig';
  493. else
  494. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  495. format = '1D';
  496. else
  497. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  498. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  499. end
  500. end
  501. end
  502. end % for (rr = 1:1:sz)
  503. end % for (kk = 1:1:FL(3).N_level)
  504. end % for (jj = 1:1:FL(2).N_level)
  505. end % for (ii = 1:1:FL(1).N_level)
  506. case 4,
  507. for (ii = 1:1:FL(1).N_level),
  508. for (jj = 1:1:FL(2).N_level),
  509. for (kk = 1:1:FL(3).N_level),
  510. for (ll = 1:1:FL(4).N_level),
  511. fprintf (2,'\nFor factor %c (%s) at level %i (%s),', 64+1, FL(1).expr, ii, FL(1).level(ii).expr);
  512. fprintf (2,'\n factor %c (%s) at level %i (%s),', 64+2, FL(2).expr, jj, FL(2).level(jj).expr);
  513. fprintf (2,'\n factor %c (%s) at level %i (%s),', 64+3, FL(3).expr, kk, FL(3).level(kk).expr);
  514. fprintf (2,'\n factor %c (%s) at level %i (%s),', 64+3, FL(3).expr, kk, FL(4).level(ll).expr);
  515. sz = input('\nsample size is: ');
  516. fprintf (2,'\nProvide those %i input files:\n', sz);
  517. for (rr = 1:1:sz),
  518. FI = FI + 1;
  519. GP(1, FI) = {FL(1).level(ii).expr};
  520. GP(2, FI) = {FL(2).level(jj).expr};
  521. GP(3, FI) = {FL(3).level(kk).expr};
  522. GP(4, FI) = {FL(4).level(ll).expr};
  523. flg = 0;
  524. while flg == 0,
  525. fprintf (2,'No. %i file ', rr);
  526. file(FI).nm = input('is: ', 's');
  527. fid = fopen (file(FI).nm,'r');
  528. if (fid == -1), flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  529. else flg = 1; fclose (fid); end
  530. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  531. format = 'tlrc';
  532. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  533. format = 'orig';
  534. else
  535. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  536. format = '1D';
  537. else
  538. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  539. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  540. end
  541. end
  542. end
  543. end % for (rr = 1:1:sz)
  544. end % for (ll = 1:1:FL(4).N_level)
  545. end % for (kk = 1:1:FL(3).N_level)
  546. end % for (jj = 1:1:FL(2).N_level)
  547. end % for (ii = 1:1:FL(1).N_level)
  548. end % switch NF
  549. if (ntot == FI), fprintf (2,'\n%i input files have been read in. \n', FI);
  550. else fprintf(2,'Error: Total number of files do not match up. \n');
  551. while (1); fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  552. end
  553. end % if (dsgn == 1)
  554. if ((NF == 3 & dsgn == 3)),
  555. % acc = 0;
  556. FI = 0; % File index
  557. % if (unbalanced.type == 1),
  558. for (i = 1:1:FL(1).N_level),
  559. for (j = 1:1:FL(2).N_level),
  560. for (k = 1:1:FL(3).UL(i).N_level),
  561. % FI = acc + (j-1)*FL(3).UL(i).N_level + k; % file index
  562. % Create a matrix for group indices
  563. % GP(1, FI) = {FL(1).level(i).expr};
  564. % GP(2, FI) = {FL(2).level(j).expr};
  565. % GP(3, FI) = {FL(3).UL(i).n(k).expr};
  566. for (r = 1:1:FL(NF+1).N_level), % if there is any repeated observations
  567. FI = FI + 1;
  568. GP(1, FI) = {FL(1).level(i).expr};
  569. GP(2, FI) = {FL(2).level(j).expr};
  570. GP(3, FI) = {FL(3).UL(i).n(k).expr};
  571. flg = 0;
  572. while flg == 0,
  573. fprintf (2,'\n(%i) factor combination:\n', FI);
  574. for (m=1:1:NF),
  575. fprintf('\tfactor %c (%s) at level %s \n', 64+m, FL(m).expr, char(GP(m, FI)));
  576. end
  577. fprintf('\tat repeat %i \n', r);
  578. file(FI).nm = input('is: ', 's');
  579. fid = fopen (file(FI).nm,'r');
  580. if (fid == -1),
  581. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  582. else flg = 1; fclose (fid); end
  583. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  584. format = 'tlrc';
  585. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  586. format = 'orig';
  587. else
  588. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  589. format = '1D';
  590. else
  591. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  592. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  593. end
  594. end
  595. end
  596. end % for (r = 1:1:FL(NF+1).N_level),
  597. end % k
  598. end % j
  599. % acc = FI;
  600. end % i
  601. % end % if (unbalanced.type == 1),
  602. end % if ((NF == 3 & dsgn == 3))
  603. if ((NF == 4 & dsgn == 3)),
  604. % acc = 0;
  605. % if (unbalanced.type == 1),
  606. FI = 0; % File index
  607. for (i = 1:1:FL(1).N_level),
  608. for (j = 1:1:FL(2).N_level),
  609. for (k = 1:1:FL(3).N_level),
  610. for (l = 1:1:FL(4).UL(i).N_level),
  611. % FI = acc + (j-1)*FL(3).N_level*FL(4).UL(i).N_level+(k-1)*FL(4).UL(i).N_level + l; % file index
  612. % Create a matrix for group indices
  613. % GP(1, FI) = {FL(1).level(i).expr};
  614. % GP(2, FI) = {FL(2).level(j).expr};
  615. % GP(3, FI) = {FL(3).level(k).expr};
  616. % GP(4, FI) = {FL(4).UL(i).n(l).expr};
  617. for (r = 1:1:FL(NF+1).N_level),
  618. FI = FI +1;
  619. GP(1, FI) = {FL(1).level(i).expr};
  620. GP(2, FI) = {FL(2).level(j).expr};
  621. GP(3, FI) = {FL(3).level(k).expr};
  622. GP(4, FI) = {FL(4).UL(i).n(l).expr};
  623. flg = 0;
  624. while flg == 0,
  625. fprintf (2,'\n(%i) factor combination:\n', FI);
  626. for (m=1:1:NF),
  627. fprintf('\tfactor %c (%s) at level %s \n', 64+m, FL(m).expr, char(GP(m, FI)));
  628. end
  629. fprintf('\tat repeat %i \n', r);
  630. file(FI).nm = input('is: ', 's');
  631. fid = fopen (file(FI).nm,'r');
  632. if (fid == -1),
  633. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  634. else flg = 1; fclose (fid); end
  635. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  636. format = 'tlrc';
  637. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  638. format = 'orig';
  639. else
  640. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  641. format = '1D';
  642. else
  643. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  644. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  645. end
  646. end
  647. end
  648. end % for (r = 1:1:FL(NF+1).N_level),
  649. end
  650. end
  651. end
  652. % acc = FI;
  653. end
  654. % end % if (unbalanced.type == 1),
  655. end % if ((NF == 4 & dsgn == 3))
  656. if ((NF == 4 & dsgn == 5)), % CXD(AXB)
  657. FI = 0; % File index
  658. for (i = 1:1:FL(1).N_level),
  659. for (j = 1:1:FL(2).N_level),
  660. for (k = 1:1:FL(3).N_level),
  661. for (l = 1:1:FL(4).UL(i, j).N_level),
  662. for (r = 1:1:FL(NF+1).N_level),
  663. FI = FI +1;
  664. GP(1, FI) = {FL(1).level(i).expr};
  665. GP(2, FI) = {FL(2).level(j).expr};
  666. GP(3, FI) = {FL(3).level(k).expr};
  667. GP(4, FI) = {FL(4).UL(i, j).n(l).expr};
  668. flg = 0;
  669. while flg == 0,
  670. fprintf (2,'\n(%i) factor combination:\n', FI);
  671. for (m=1:1:NF),
  672. fprintf('\tfactor %c (%s) at level %s \n', 64+m, FL(m).expr, char(GP(m, FI)));
  673. end
  674. fprintf('\tat repeat %i \n', r);
  675. file(FI).nm = input('is: ', 's');
  676. fid = fopen (file(FI).nm,'r');
  677. if (fid == -1),
  678. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(FI).nm);
  679. else flg = 1; fclose (fid); end
  680. if isempty(strfind(file(FI).nm, 'tlrc')) == 0
  681. format = 'tlrc';
  682. elseif isempty(strfind(file(FI).nm, 'orig')) == 0
  683. format = 'orig';
  684. else
  685. if isempty(strfind(file(FI).nm, '1D')) == 0 % if 1D file (surface data)
  686. format = '1D';
  687. else
  688. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(FI).nm);
  689. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  690. end
  691. end
  692. end
  693. end % for (r = 1:1:FL(NF+1).N_level),
  694. end
  695. end
  696. end
  697. end
  698. end % if ((NF == 4 & dsgn == 5))
  699. %end % (unbalanced.yes == 1),
  700. else % Balanced designs
  701. fprintf('\nPlease provide input files.');
  702. for (i=1:1:ntot),
  703. %Ziad Saad modified Matlab function ind2sub for the purpose here
  704. %Converting the index into multiple subscripts
  705. %I want vary the levels starting the last factor in stead of the first.
  706. [err, file(i).v] = gind2sub (fliplr(sz), i); %flip before subscripting. Doing flip because
  707. %I want vary the levels starting the last factor in stead of the first.
  708. scpt = fliplr(file(i).v); %flip back to restore the original order
  709. flg = 0;
  710. while flg == 0,
  711. fprintf (2,'\n(%i) factor combination:\n', i);
  712. for (k=1:1:NF),
  713. fprintf('\tfactor %c (%s) at level %i (%s) \n', 64+k, FL(k).expr, scpt(k), FL(k).level(scpt(k)).expr);
  714. GP(k, i) = {FL(k).level(scpt(k)).expr};
  715. end
  716. fprintf('\tat repeat %i \n', scpt(NF+1));
  717. file(i).nm = input('is: ', 's');
  718. fid = fopen (file(i).nm,'r');
  719. if (fid == -1),
  720. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(i).nm);
  721. else flg = 1; fclose (fid); end
  722. if isempty(strfind(file(i).nm, 'tlrc')) == 0
  723. format = 'tlrc';
  724. elseif isempty(strfind(file(i).nm, 'orig')) == 0
  725. format = 'orig';
  726. else
  727. if isempty(strfind(file(i).nm, '1D')) == 0 % if 1D file (surface data)
  728. format = '1D';
  729. else
  730. while (1); fprintf(2,'Error: format of file %s is incorrect!\n', file(i).nm);
  731. fprintf(2,'Halted: Ctrl+c to exit'); pause; end
  732. end
  733. end
  734. end
  735. end
  736. end % if (unbalanced.yes == 1),
  737. end % if (file_format == 1),
  738. if (file_format == 0), % unused now!
  739. if (unbalanced.yes == 1), % Try 4-way design 3 first
  740. if ((NF == 4 & dsgn == 3)),
  741. acc = 0;
  742. if (unbalanced.type == 1),
  743. for (i = 1:1:FL(1).N_level),
  744. for (j = 1:1:FL(2).N_level),
  745. for (k = 1:1:FL(3).N_level),
  746. for (l = 1:1:FL(4).UL(i).N_level),
  747. FI = acc + (j-1)*FL(3).N_level*FL(4).UL(i).N_level+(k-1)*FL(4).UL(i).N_level + l; % file index
  748. GP(1, FI) = {FL(1).level(i).expr};
  749. GP(2, FI) = {FL(2).level(j).expr};
  750. GP(3, FI) = {FL(3).level(k).expr};
  751. GP(4, FI) = {FL(4).UL(i).n(l).expr};
  752. end
  753. end
  754. end
  755. acc = FI;
  756. end
  757. for (m = 1:1:FL(1).N_level),
  758. counter(m) = 0;
  759. end %Just initiation
  760. for (i=1:1:file_num),
  761. flg = 0;
  762. while flg == 0,
  763. fprintf('Input file for subject #%i ', i);
  764. file(i).nm = input('is: ', 's');
  765. fid = fopen (file(i).nm,'r');
  766. fprintf('\tThe corresponding level of factor 1 for subject %i ', i);
  767. file(i).F1L = input('is: '); % level for factor 1
  768. if (isnumeric(file(i).F1L) == 0 | isempty(file(i).F1L)),
  769. flg = 0; fprintf(2,'Error: the input is not a number. Please try it again.\n');
  770. end
  771. for (m = 1:1:FL(1).N_level),
  772. if (file(i).F1L == m),
  773. counter(m) = counter(m) + 1;
  774. file(i).cntr(m) = counter(m); % Any better way to implement this without the temporary array of counter(j)?
  775. end % set a counter of 4th factor level marker for later use
  776. end
  777. if (fid == -1),
  778. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(i).nm);
  779. else flg = 1; fclose (fid); end
  780. end
  781. end % for (i=1:1:file_num),
  782. fprintf('\nIt is assumed that all files have the same subbrik order.');
  783. fprintf('\nFor each subbrik of an input file, specify the factor level of 2nd and 3rd factors. \n');
  784. for (j = 1:1:file_SB),
  785. fprintf('\n\tFor subbrik %i: \n', j-1);
  786. for (k = 1:1:(NF-2)), % Here we only keep record of 2nd and 3rd factor levels
  787. fprintf('\t\tlevel of factor %c (%s) ', 65+k, FL(k+1).expr);
  788. SB(j).lv(k) = input('is: ');
  789. end %for (j = 1:1:file_SB),
  790. end % for (j = 1:1:file_SB),
  791. end % if (unbalanced.type == 1),
  792. end % if ((NF == 4 & dsgn == 3))
  793. %end % (unbalanced.yes == 1),
  794. else
  795. for (i=1:1:ntot),
  796. [err, file(i).v] = gind2sub (fliplr(sz), i); %flip before subscripting. Doing flip because
  797. %I want vary the levels starting the last factor in stead of the first.
  798. scpt = fliplr(file(i).v); %flip back to restore the original order
  799. for (k=1:1:NF),
  800. GP(k, i) = {FL(k).level(scpt(k)).expr};
  801. end
  802. end
  803. for (j = 1:1:FL(1).N_level),
  804. counter(j) = 0;
  805. end %Just initiation
  806. for (i = 1:1:file_num),
  807. flg = 0;
  808. while flg == 0,
  809. fprintf('Input file for subject #%i ', i);
  810. file(i).nm = input('is: ', 's');
  811. fid = fopen (file(i).nm,'r');
  812. fprintf('\tThe corresponding level of factor 1 for subject %i ', i);
  813. file(i).F1L = input('is: '); % level for factor 1
  814. if (isnumeric(file(i).F1L) == 0 | isempty(file(i).F1L)),
  815. flg = 0; fprintf(2,'Error: the input is not a number. Please try it again.\n');
  816. end
  817. for (j = 1:1:FL(1).N_level),
  818. if (file(i).F1L == j),
  819. counter(j) = counter(j) + 1;
  820. file(i).cntr(j) = counter(j); % Any better way to implement this without the temporary array of counter(j)?
  821. end % set a counter of 4th factor level marker for later use
  822. end
  823. if (fid == -1),
  824. flg = 0; fprintf(2,'Error: File %s does not exist. Please try it again. \n', file(i).nm);
  825. else flg = 1; fclose (fid); end
  826. end
  827. end
  828. fprintf('\nIt is assumed that all files have the same subbrik order.');
  829. fprintf('\nFor each subbrik of an input file, specify the factor level of 2nd and 3rd factors. \n');
  830. for (j = 1:1:file_SB),
  831. fprintf('\n\tFor subbrik %i: \n', j-1);
  832. for (k = 1:1:(NF-2)), %Here we only keep record of 2nd and 3rd factor levels
  833. fprintf('\t\tlevel of factor %c (%s) ', 65+k, FL(k+1).expr);
  834. SB(j).lv(k) = input('is: ');
  835. end
  836. end
  837. end % if (unbalanced.yes == 1)
  838. end % if (file_format == 0),
  839. %Obtain output file name
  840. flg = 0;
  841. while flg == 0,
  842. OutFN = input('\nOutput file name (in bucket format): ', 's');
  843. OutFull = sprintf('%s+%s.HEAD', OutFN, format);
  844. fid2 = fopen(OutFull,'r');
  845. if (fid2 ~= -1),
  846. flg = 0; fprintf(2,'Error: File %s exists already. Please give another name. \n', OutFN);
  847. else flg = 1; end
  848. end
  849. % Gather contrast information: Everything stored in structure Contr
  850. flg = 0;
  851. while flg == 0,
  852. Contr.do = input('\nAny contrast test (1 - Yes, 0 - No)? ');
  853. if (Contr.do ~= 0 & Contr.do ~= 1),
  854. flg = 0; fprintf(2,'Error: invalid answer. Try it again. \n', OutFN);
  855. else flg = 1; end
  856. end
  857. if (Contr.do == 0),
  858. Contr.ord1.tot = 0; % assign these for output later
  859. Contr.ord2.tot = 0;
  860. Contr.ord3.tot = 0;
  861. Contr.ord4.tot = 0;
  862. else
  863. fprintf('\nNow coding contrasts:\n');
  864. fprintf('\n\t1. Each contrast should contain at least 2 terms;');
  865. fprintf('\n\t2. Each term in a contrast should have %i character(s), \n', NF);
  866. for (i = 1:1:NF), fprintf('\tNo. %i character corresponds to the level of factor %c (%s),\n', i, 'A'+i-1, FL(i).expr); end
  867. fprintf('\n\tUse 0 if a factor is collapsed.\n');
  868. fprintf('\n\tIf a factor level is smaller than 9, use its ordinal number;');
  869. fprintf('\n\tIf a factor level is bigger than 9, use a, b, c, ... (no capitals) for 10, 11, 12, ... \n');
  870. fprintf('\n\t3. All weights/coeficients in a contrast have to add up to zero.\n');
  871. if (NF == 1),
  872. % 1st order contrasts
  873. flg = 0;
  874. while flg == 0,
  875. fprintf('\n1st order contrasts have %i factor(s) collapsed.\n', NF-1);
  876. Contr.ord1.tot = input('\nHow many 1st-order contrasts? (0 if none) ');
  877. if (isnumeric(Contr.ord1.tot) == 0 | Contr.ord1.tot < 0),
  878. flg = 0; fprintf(2,'Error: inapproriate input. Please try again.\n');
  879. else flg = 1;
  880. end
  881. end
  882. % fprintf('\nUse factor level to code each term in a contrast. For example, 1 means the factor is at first level.\n');
  883. for (i = 1:1:Contr.ord1.tot),
  884. fprintf('\nLabel for 1st order contrast No. %i ', i);
  885. Contr.ord1.label(i).nm = input('is: ', 's');
  886. flg = 0;
  887. while flg == 0,
  888. Contr.ord1.cnt(i).NT = input('How many terms are involved in this contrast? '); % NT = number of terms involved in this contrast
  889. if (isnumeric(Contr.ord1.cnt(i).NT) == 0 | Contr.ord1.cnt(i).NT < 2),
  890. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n\n');
  891. else flg = 1; end
  892. end
  893. flg0 = 0;
  894. while flg0 == 0,
  895. for (j = 1:1:Contr.ord1.cnt(i).NT),
  896. flg = 0;
  897. while flg == 0,
  898. fprintf('Factor index for No. %i term ', j);
  899. Contr.ord1.cnt(i).code(j).str = input('is (e.g., 2): ', 's');
  900. if (length(Contr.ord1.cnt(i).code(j).str) ~= NF),
  901. flg = 0; fprintf(2,'Error: invalid input. Try it again. \n', OutFN);
  902. else flg = 1; end
  903. end
  904. Contr.ord1.cnt(i).coef(j) = input('Corresponding coefficient (e.g., 1 or -1): ');
  905. end
  906. if (sum(Contr.ord1.cnt(i).coef) ~= 0),
  907. flg0 = 0; fprintf(2,'\nError: All the coeficients of a contrast have to sum up to 0! Try again...\n\n');
  908. else flg0 = 1; end
  909. end
  910. end
  911. Contr.ord2.tot = 0;
  912. Contr.ord3.tot = 0;
  913. Contr.ord4.tot = 0;
  914. fprintf(1,'Done with 1st order contrast information.\n');
  915. end
  916. if (NF == 2),
  917. % 1st order contrasts ONLY at this point
  918. flg = 0;
  919. while flg == 0,
  920. fprintf('\n1st order contrasts have %i factor(s) collapsed.\n', NF-1);
  921. Contr.ord1.tot = input('\nHow many 1st-order contrasts? (0 if none) ');
  922. if (isnumeric(Contr.ord1.tot) == 0 | Contr.ord1.tot < 0),
  923. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n');
  924. else flg = 1;
  925. end
  926. end
  927. % fprintf('\nUse factor level to code each term in a contrast. For example, 1 means the factor is at first level.\n');
  928. for (i = 1:1:Contr.ord1.tot),
  929. fprintf('\nLabel for 1st order contrast No. %i ', i);
  930. Contr.ord1.label(i).nm = input('is: ', 's');
  931. flg = 0;
  932. while flg == 0,
  933. Contr.ord1.cnt(i).NT = input('How many terms are involved in this contrast? '); % NT = number of terms involved in this contrast
  934. if (isnumeric(Contr.ord1.cnt(i).NT) == 0 | Contr.ord1.cnt(i).NT < 2),
  935. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n\n');
  936. else flg = 1; end
  937. end
  938. flg0 = 0;
  939. while flg0 == 0,
  940. for (j = 1:1:Contr.ord1.cnt(i).NT),
  941. flg = 0;
  942. while flg == 0,
  943. fprintf('Factor index for No. %i term ', j);
  944. Contr.ord1.cnt(i).code(j).str = input('is (e.g., 10): ', 's');
  945. if (length(Contr.ord1.cnt(i).code(j).str) ~= NF),
  946. flg = 0; fprintf(2,'\nError: invalid input. Try it again. \n', OutFN);
  947. else flg = 1; end
  948. end
  949. Contr.ord1.cnt(i).coef(j) = input('Corresponding coefficient (e.g., 1 or -1): ');
  950. end
  951. if (sum(Contr.ord1.cnt(i).coef) ~= 0),
  952. flg0 = 0; fprintf(2,'\nError: All the coeficients of a contrast have to sum up to 0! Try again...\n\n');
  953. else flg0 = 1; end
  954. end
  955. end
  956. fprintf(1,'Done with 1st order contrast information.\n');
  957. % 2nd order contrasts
  958. flg = 0;
  959. while flg == 0,
  960. fprintf('\n2nd order contrasts have %i factor(s) collapsed.\n', NF-2);
  961. fprintf('\nNotice: Contrasts for random factor are NOT feasible.\n');
  962. Contr.ord2.tot = input('\nHow many 2nd-order contrasts? (0 if none) ');
  963. if (isnumeric(Contr.ord2.tot) == 0 | Contr.ord2.tot < 0),
  964. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n');
  965. else flg = 1;
  966. end
  967. end
  968. % fprintf('\nUse factor level to code each term in a contrast. For example, 12 means ');
  969. % fprintf('\nthe 1st and 2nd factors are at their first and second level respectively.\n');
  970. for (i = 1:1:Contr.ord2.tot),
  971. fprintf('\nLabel for 2nd order contrast No. %i: ', i);
  972. Contr.ord2.label(i).nm = input('is: ', 's');
  973. flg = 0;
  974. while flg == 0,
  975. Contr.ord2.cnt(i).NT = input('How many terms are involved in this contrast? '); % NT = number of terms involved in this contrast
  976. if (isnumeric(Contr.ord2.cnt(i).NT) == 0 | Contr.ord2.cnt(i).NT < 2),
  977. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n\n');
  978. else flg = 1; end
  979. end
  980. flg0 = 0;
  981. while flg0 == 0,
  982. for (j = 1:1:Contr.ord2.cnt(i).NT),
  983. flg = 0;
  984. while flg == 0,
  985. fprintf('Factor index for No. %i term ', j);
  986. Contr.ord2.cnt(i).code(j).str = input('is (e.g., 12): ', 's');
  987. if (length(Contr.ord2.cnt(i).code(j).str) ~= NF),
  988. flg = 0; fprintf(2,'\nError: invalid input. Try it again. \n', OutFN);
  989. else flg = 1; end
  990. end
  991. Contr.ord2.cnt(i).coef(j) = input('Corresponding coefficient (e.g., 1 or -1): ');
  992. end
  993. if (sum(Contr.ord2.cnt(i).coef) ~= 0),
  994. flg0 = 0; fprintf(2,'\nError: All the coeficients of a contrast have to sum up to 0! Try again...\n\n');
  995. else flg0 = 1; end
  996. end
  997. end
  998. Contr.ord3.tot = 0;
  999. Contr.ord4.tot = 0;
  1000. fprintf(1,'Done with 2nd order contrast information.\n');
  1001. end
  1002. if (NF == 3 | NF == 4),
  1003. % 1st order contrasts
  1004. flg = 0;
  1005. while flg == 0,
  1006. fprintf('\n1st order contrasts have %i factor(s) collapsed.\n', NF-1);
  1007. %fprintf('\t1. Simple mean for a factor level, such as [1 0 0];\n');
  1008. %fprintf('\t2. Difference between two factor levels, such as [1 -1 0];\n');
  1009. %fprintf('\t3. Linear combination of factor levels, such as [0.5 0.5 -1];\n');
  1010. fprintf('\nNotice: Contrasts for random factor are NOT feasible.\n');
  1011. Contr.ord1.tot = input('\nHow many 1st-order contrasts? (0 if none) ');
  1012. if (isnumeric(Contr.ord1.tot) == 0 | Contr.ord1.tot < 0),
  1013. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n');
  1014. else flg = 1;
  1015. end
  1016. end
  1017. % fprintf('\nUse factor level to code each term in a contrast. For example, 0100 means the first, third ');
  1018. % fprintf('\nand fourth factors are collapsed while 2nd factor is at first level.\n');
  1019. for (i = 1:1:Contr.ord1.tot),
  1020. fprintf('\nLabel for 1st order contrast No. %i ', i);
  1021. Contr.ord1.label(i).nm = input('is: ', 's');
  1022. flg = 0;
  1023. while flg == 0,
  1024. Contr.ord1.cnt(i).NT = input('How many terms are involved in this contrast? '); % NT = number of terms involved in this contrast
  1025. if (isnumeric(Contr.ord1.cnt(i).NT) == 0 | Contr.ord1.cnt(i).NT < 2),
  1026. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n\n');
  1027. else flg = 1; end
  1028. end
  1029. flg0 = 0;
  1030. while flg0 == 0,
  1031. for (j = 1:1:Contr.ord1.cnt(i).NT),
  1032. flg = 0;
  1033. while flg == 0,
  1034. fprintf('Factor index for No. %i term ', j);
  1035. Contr.ord1.cnt(i).code(j).str = input('is (e.g., 0020): ', 's');
  1036. if (length(Contr.ord1.cnt(i).code(j).str) ~= NF),
  1037. flg = 0; fprintf(2,'\nError: invalid input. Try it again. \n', OutFN);
  1038. else flg = 1; end
  1039. end
  1040. Contr.ord1.cnt(i).coef(j) = input('Corresponding coefficient (e.g., 1 or -1): ');
  1041. end
  1042. if (sum(Contr.ord1.cnt(i).coef) ~= 0),
  1043. flg0 = 0; fprintf(2,'\nError: All the coeficients of a contrast have to sum up to 0! Try again...\n\n');
  1044. else flg0 = 1; end
  1045. end
  1046. end
  1047. fprintf(1,'Done with 1st order contrast information.\n');
  1048. % 2nd order contrasts
  1049. flg = 0;
  1050. while flg == 0,
  1051. fprintf('\n2nd order contrasts have %i factor(s) collapsed.\n', NF-2);
  1052. fprintf('\nNotice: Contrasts for random factor are NOT feasible.\n');
  1053. Contr.ord2.tot = input('\nHow many 2nd-order contrasts? (0 if none) ');
  1054. if (isnumeric(Contr.ord2.tot) == 0 | Contr.ord2.tot < 0),
  1055. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n');
  1056. else flg = 1;
  1057. end
  1058. end
  1059. % fprintf('\nUse factor level to code each term in a contrast. For example, 0120 means both the first ');
  1060. % fprintf('\nand fourth factors are collapsed while 2nd and 3rd factors are at first and second level respectively.\n');
  1061. for (i = 1:1:Contr.ord2.tot),
  1062. fprintf('\nLabel for 2nd order contrast No. %i ', i);
  1063. Contr.ord2.label(i).nm = input('is: ', 's');
  1064. flg = 0;
  1065. while flg == 0,
  1066. Contr.ord2.cnt(i).NT = input('How many terms are involved in this contrast? '); % NT = number of terms involved in this contrast
  1067. if (isnumeric(Contr.ord2.cnt(i).NT) == 0 | Contr.ord2.cnt(i).NT < 2),
  1068. flg = 0; fprintf(2,'\nError: inapproriate input. Please try again.\n\n');
  1069. else flg = 1; end
  1070. end
  1071. flg0 = 0;
  1072. wh

Large files files are truncated, but you can click here to view the full file