function [X,select,names,xyz]=conn_designmatrix(confounds,X1,X2,nconfounds) select=[];X=[];names={};xyz={}; if iscell(confounds), temp=confounds{1};tempfields=fieldnames(temp);for n1=2:numel(confounds),for n2=1:numel(tempfields), temp.(tempfields{n2})=cat(2,temp.(tempfields{n2}),confounds{n1}.(tempfields{n2})); end; end; confounds=temp; valid=ones(numel(confounds.names),1); for n1=1:numel(confounds.names), if valid(n1) idx=intersect(strmatch(confounds.names{n1},confounds.names,'exact'),strmatch(confounds.types{n1},confounds.types,'exact')); confounds.dimensions{n1}=max(cat(1,confounds.dimensions{idx}),[],1); confounds.deriv{n1}=max(cat(1,confounds.deriv{idx}),[],1); valid(idx(2:end))=0; end end if any(~valid) idx=find(valid); for n2=1:numel(tempfields), confounds.(tempfields{n2})=confounds.(tempfields{n2})(idx); end end end if nargin>3, select=cell(size(nconfounds)); end for n1=1:length(confounds.names), switch(confounds.types{n1}), case 'roi', idx=strmatch(confounds.names{n1},X1.names,'exact'); if isempty(idx)||length(idx)>1, error('Mismatch info: Run SETUP again!'); end x=X1.data{idx}(:,1:min(confounds.dimensions{n1}(1),size(X1.data{idx},2))); if isfield(X1,'d1data'), d1x=X1.d1data{idx}(:,1:min(confounds.dimensions{n1}(1),size(X1.d1data{idx},2))); else d1x=convn(cat(1,x(1,:),x,x(end,:)),[1;0;-1],'valid'); end if isfield(X1,'d2data'), d2x=X1.d2data{idx}(:,1:min(confounds.dimensions{n1}(1),size(X1.d2data{idx},2))); else d2x=convn(cat(1,d1x(1,:),d1x,d1x(end,:)),[1;0;-1],'valid'); end dx={d1x,d2x}; case 'cov', idx=strmatch(confounds.names{n1},X2.names,'exact'); if isempty(idx)||length(idx)>1, error('Mismatch info: Run SETUP again!'); end x=X2.data{idx}(:,1:min(confounds.dimensions{n1}(1),size(X2.data{idx},2))); if isfield(X2,'d1data'), d1x=X2.d1data{idx}(:,1:min(confounds.dimensions{n1}(1),size(X2.d1data{idx},2))); else d1x=convn(cat(1,x(1,:),x,x(end,:)),[1;0;-1],'valid'); end if isfield(X2,'d2data'), d2x=X2.d2data{idx}(:,1:min(confounds.dimensions{n1}(1),size(X2.d2data{idx},2))); else d2x=convn(cat(1,d1x(1,:),d1x,d1x(end,:)),[1;0;-1],'valid'); end dx={d1x,d2x}; end if ~isempty(x), X=cat(2,X,x); for n0=1:size(x,2),names{end+1}=[confounds.names{n1},'_',num2str(1),'_',num2str(n0)];end if strcmp(confounds.types{n1},'roi')&&isfield(X1,'xyz'), for n0=1:size(x,2),xyz{end+1}=X1.xyz{idx}+(n0-1)*(confounds.deriv{n1}+1);end; else for n0=1:size(x,2),xyz{end+1}=nan(1,3);end; end if nargin>3, for n0=1:length(nconfounds), if any(n1==nconfounds{n0}), select{n0}=cat(2,select{n0},ones(1,size(x,2))); else select{n0}=cat(2,select{n0},zeros(1,size(x,2))); end end; end for n2=1:min(confounds.deriv{n1},numel(dx)), %x=convn(cat(1,x(1,:),x,x(end,:)),[1;0;-1],'valid'); x=dx{n2}; X=cat(2,X,x); for n0=1:size(x,2),names{end+1}=[confounds.names{n1},'_',num2str(n2+1),'_',num2str(n0)];end if strcmp(confounds.types{n1},'roi')&&isfield(X1,'xyz'),for n0=1:size(x,2),xyz{end+1}=X1.xyz{idx}+(n0-1)*(confounds.deriv{n1}+1)+n2;end; else for n0=1:size(x,2),xyz{end+1}=nan(1,3);end; end if nargin>3, for n0=1:length(nconfounds), if any(n1==nconfounds{n0}), select{n0}=cat(2,select{n0},ones(1,size(x,2))); else select{n0}=cat(2,select{n0},zeros(1,size(x,2))); end end; end end end end N=size(X,1); if ~N, N=size(X1.data{1},1); end X=[ones(N,1),X]; if nargin>3, for n0=1:length(nconfounds),select{n0}=[0,select{n0}];end; end