How do I loop over subjects in a script with 3d arrays?

Dear experts, I have written a script to read .nii files (volumetric 3d images), one of it being binary. I made sure the dimensions are equal, because I needed to multiply the arrays and calculate the overlap. The script works. However, I want the script to loop over 77 subjects in a directory.
This is the script for one subject:
lobes = spm_read_vol(spm_vol('xx/xx/x.nii')); %this is a mask with dim [51 75 55]
seg = spm_read_vols(spm_vol('/xx/xx/Images/wseg.nii')); %this is a binary 3D object with dim [153 225 165]
segf=flipud(seg); %I used this, because somehow my object is mirrored
seg2=griddedInterpolant(segf);
seg3=seg2({linspace(1, size(segf,1),51),linspace(1,size(segf,2),75),linspace(1,size(segf,3),55)}); %resizes the original 3D image to get the same dimensions as the mask image (lobes).
for j=1:55
locseg(:,:,j)=lobes(:,:,j).*seg3(:,:,j); %multiply 3D arrays with a for loop
end
location=mode(nonzeros(locseg)) %final output of interest
ps.: I don't have the image processing toolbox, and I am 100% sure this is not the easiest way to get the wanted output, but my options are limited.
Any help is appreciated!

5 Comments

The only tricky part is to get the complete filepath. do both seg and lobes vary for each subject?
lobes doesn't vary. Seg does. I tried this:
wd='/Data/users/xx/xx/'; ID_CODES=dir([wd '*.MRI.1']); %all subjects have dir ending with .MRI.1
seg=cell(length(ID_CODES), 1); % predefines the size of the matrix
for i = 1:length(ID_CODES) % Loop over patients
dp=char(strcat('/Data/users/xx/xx/',ID_CODES(i).name,'/Images/wseg.nii')); seg(i)= spm_read_vols(spm_vol({char(strcat(dp))})); end
but I get the error: Struct contents reference from a non-struct array object.
Error in spm_read_vols (line 30) Y = zeros([V(1).dim(1:3),n]); %-image data matrix
I understand it has something to do with my matrix, but I have no idea how to fix this.
Remove this.
seg=cell(length(ID_CODES), 1); % predefines the size of the matrix
Use this for dp(More clean)
dp=fullfile(ID_CODES(i).folder,ID_CODES(i).name,'/Images/wseg.nii');
as seg is 3D array now it will becomes 4D array
seg(:,:,:,i)=spm_read_vols(spm_vol(dp));
Thank you! :) seg is now a 4-D double array.
but I'm very new into this and I am wondering how this will affect the rest of my script.
if true
segf=flipud(seg); %I used this, because somehow my object is mirrored
seg2=griddedInterpolant(segf);
seg3=seg2({linspace(1, size(segf,1),51),linspace(1,size(segf,2),75),linspace(1,size(segf,3),55)}); %resizes the original 3D image to get the same dimensions as the mask image (lobes).
for j=1:55
locseg(:,:,j)=lobes(:,:,j).*seg3(:,:,j); %multiply 3D arrays with a for loop
end
location=mode(nonzeros(locseg))
simply changing segx(:,:,i) to segx(:,:,:,i) does not seem to do the trick and I am still kind of lost in looping.
as you don't need any variable other than location.
%get all things for dp
for 1=1:#subjects
dp=...
seg= read(dp) (don't append)
rest of the code as in your question
location(:,:,:,i)=mode(...) (put that many colons as dimension of the location w/o looping)
end

Sign in to comment.

Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!