Code

From Psych 221 Image Systems Engineering
Jump to navigation Jump to search

%%Source: http://sites.google.com/site/mvlombardo/matlab-tutorials

%%comments that don't start with 'Grace:' were written by website author

SubjArray={'F-1', 'F-2', 'F-4', 'I-6', 'H-5', 'C-1', 'C-4', 'C-5', 'C-7', 'J-5'}; %Grace: change subject names here! SubjArray2={'F1', 'F2', 'F4', 'I6', 'H5', 'C1', 'C4', 'C5', 'C7', 'J5'}; %Grace: ...and here, if your segmentation file's subject names were different Datamat=zeros(157, 189, 136, length(SubjArray));  % Grace: creates matrix where last number is subject index number

for i=1:length(SubjArray)

datadir = ['/Users/grace/Desktop/PSYCH204A/' SubjArray{i} '/']; % datadir = ['/Users/grace/Desktop/PSYCH204A/']; fname = ['w' SubjArray2{i} '_NAcc_seg_clean.nii']; Data2Read = fullfile(datadir,fname);

% What I've done in Step 1 is to construct a filename such as /data/fMRI/12001.nii from its filepath (the variable called datadir) and filename (the variable called fname). Both these variables are string variables and I've used the function fullfile to essentially concatenate these two strings together. The end result is a string variable called Data2Read, which is essential a string that looks like this: /data/fMRI/12001.nii


% Step 2. Now pass Data2Read into the function spm_vol in order to read in the header information for the file. Note that the spm_vol function comes with the SPM toolbox in Matlab that is a standard software package for neuroimagers. Having SPM is essential for this part. HeaderInfo = spm_vol(Data2Read);

% In Step 2, the header information for the data you want to read in is now in your workspace. This is required for Step 3, where you will use the header information to actually read in the entire data.

% Step 3. Now use spm_read_vols to read in the data Data = spm_read_vols(HeaderInfo);


%% Grace: converting non zero matrix values to 1

indx=find(Data); Data(indx)=1; Datamat(:,:,:,i)=Data; %Grace: Datamat now contains all your matrix data


% %% saving all values in file to '1' % HeaderInfo.fname = ['w' SubjArray2{i} '_NAcc_seg_allten.nii'];  % This is where you fill in the new filename % HeaderInfo.private.dat.fname = HeaderInfo.fname;  % This just replaces the old filename in another location within the header. % % % % Step 2. Now use spm_write_vol to write out the new data. You need to give spm_write_vol the new header information and corresponding data matrix % spm_write_vol(HeaderInfo,Data);

end

combinemat_g=sum(Datamat,4); %Grace: sum across subjects... now values indicate overlap across your subjects


%%% %% histogram

index2=find(combinemat_g); %G: find non zero elements, so histogram doesn't have massive bar at 0; toplot=combinemat_g(index2); hist(toplot,9); % G: hardcoded to 9 bins, because maximum overlap was 9 ylabel('# voxels with x subjects overlapping') xlabel('x - number of subjects per voxel')


figure(2) rhist(toplot,9);

ylabel('relative frequency of voxels with x subjects overlapping') xlabel('x - number of subjects per voxel')


%% figure 3: cumulative hist

figure(3) n_elements=histc(toplot, 1:9); c_elements=cumsum(n_elements); bar(1:9, c_elements); ylabel('cumulative number of voxels with overlap above x number of subjects') xlabel('x number of subjects')

%%% % % %% writing data out to nifti % % %%%% % % Step 1. Take the header information from a previous file with similar dimensions and voxel sizes and change the filename in the header. % HeaderInfo.fname = 'Combinemat_N10_allten_smooth.nii';  % This is where you fill in the new filename % HeaderInfo.private.dat.fname = HeaderInfo.fname;  % This just replaces the old filename in another location within the header. % % % Step 2. Now use spm_write_vol to write out the new data. You need to give spm_write_vol the new header information and corresponding data matrix % spm_write_vol(HeaderInfo,combinemat_g);  % where HeaderInfo is your header information for the new file, and Data is the image matrix corresponding to the image you'll be writing out.