spm_dcm_U.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. function DCM = spm_dcm_U(DCM,SPM,sess,inputs)
  2. % Insert new inputs into a DCM
  3. % FORMAT DCM = spm_dcm_U(DCM,SPM,sess,inputs)
  4. %
  5. % DCM - DCM structure or its filename
  6. % SPM - SPM structure or its filename
  7. % sess - session index (integer)
  8. % inputs - Inputs to include (cell array)
  9. %
  10. % Examples of specification of parameter 'inputs':
  11. % * without parametric modulations:
  12. % {1, 0, 1} includes inputs 1 and 3.
  13. % * with parametric modulations:
  14. % {1,0,[0 0 1],[0 1]} includes the non-modulated first input, the second
  15. % PM of the third input and the first PM of the fourth input.
  16. % Note that this cell array only has to be specified up to the last input
  17. % that is replaced.
  18. %
  19. % This function can be used, for example, to replace subject X's inputs by
  20. % subject Y's. The model can then be re-estimated without having to go
  21. % through model specification again.
  22. %__________________________________________________________________________
  23. % Copyright (C) 2003-2014 Wellcome Trust Centre for Neuroimaging
  24. % Will Penny & Klaas Enno Stephan
  25. % $Id: spm_dcm_U.m 7228 2017-11-21 12:17:03Z peter $
  26. %-Load DCM and SPM files
  27. %--------------------------------------------------------------------------
  28. if ~isstruct(DCM)
  29. DCMfile = DCM;
  30. load(DCM);
  31. end
  32. if ~isstruct(SPM)
  33. load(SPM);
  34. end
  35. %-Get session
  36. %--------------------------------------------------------------------------
  37. try
  38. Sess = SPM.Sess(sess);
  39. catch
  40. error('SPM file does not have a session %d.',sess);
  41. end
  42. %-Check numbers of inputs
  43. %--------------------------------------------------------------------------
  44. if size(DCM.c,2) ~= sum(cellfun(@nnz,inputs))
  45. error('Number of specified inputs does not match DCM.');
  46. end
  47. if numel(inputs) > numel(Sess.U)
  48. error('More inputs specified than exist in SPM.mat.');
  49. end
  50. %-Replace inputs
  51. %--------------------------------------------------------------------------
  52. U.name = {};
  53. U.u = [];
  54. U.idx = [];
  55. try
  56. U.dt = DCM.U.dt;
  57. catch
  58. U.dt = Sess.U(1).dt;
  59. end
  60. for i = 1:numel(inputs)
  61. if any(inputs{i})
  62. mo = find(inputs{i});
  63. num_regressors = size(Sess.U(i).u,2);
  64. if length(mo) > num_regressors
  65. error(['More regressors specified than exist ' ...
  66. 'for input %s in SPM.mat.'],Sess.U(i).name{1});
  67. end
  68. for j=mo
  69. U.u = [U.u Sess.U(i).u(33:end,j)];
  70. U.name{end + 1} = Sess.U(i).name{j};
  71. U.idx = [U.idx; i j];
  72. end
  73. end
  74. end
  75. DCM.U = U;
  76. %-Check inputs and outputs match up (to the nearest DCM.U.dt)
  77. %--------------------------------------------------------------------------
  78. DCM.U.dt = Sess.U(1).dt;
  79. DCM.Y.dt = SPM.xY.RT;
  80. num_inputs = size(DCM.U.u,1);
  81. input_period = DCM.U.dt*num_inputs;
  82. output_period = DCM.v*DCM.Y.dt;
  83. if round(DCM.v*DCM.Y.dt/DCM.U.dt) ~= num_inputs
  84. error(sprintf(['Input period and output period do not match.\n'...
  85. sprintf('Number of inputs=%d, input dt=%1.2f, input period=%1.2f\n',...
  86. num_inputs,DCM.U.dt,input_period) ...
  87. sprintf('Number of outputs=%d, output dt=%1.2f, output period=%1.2f\n',...
  88. DCM.v,DCM.Y.dt,output_period)]));
  89. end
  90. % Save (overwrite) DCM with replaced inputs
  91. %--------------------------------------------------------------------------
  92. if exist('DCMfile','var')
  93. save(DCMfile, 'DCM', spm_get_defaults('mat.format'));
  94. end