Hi Annabel,
As per my understanding of the question, you want to perform some operations on MRI images present with you and write it to a file.
I searched the documentation and did not find any “MRIRead” or “MRIWrite” functions. On the other hand, there is a function to read NIfTI files and write them.
Here is the documentation of “niftiread” and “niftiwrite”, the functions I have used to read and write the files.
Hence, I have provided an updated code which is working for your inputs. Also, as of now, I have not done the “subtraction”, as it is not clear to me how to perform the subtraction. You can change the line number 20 in the code as you wish to and subtract the inputs.
datadir = '';
d = dir(['*Lesion_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Lesion Masks ' ]);
d = dir(['*Wholebrain_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Wholebrain Masks ' ]);
resultspath = '';
Nd = numel(d);
for i = 1 : Nd
disp(sprintf('Working on case %d of %d: %s',i,Nd,d(i).name))
f = find(d(i).name=='_');
prefix = d(i).name(1:f(1));
lesionmask = fullfile(datadir,[prefix 'Lesion_Mask.nii.gz']);
wholebrainmask = fullfile(datadir,[prefix 'Wholebrain_Mask.nii.gz']);
MMmri = niftiread(wholebrainmask); %read wholebrain mask
LMmri = niftiread(lesionmask); % read lesion mask
subtractedMask = MMmri; %You can perform your subtraction here.
outputFilePath = fullfile(resultspath, [prefix 'Subtracted_Mask']);
% Write the result to a NIfTI file
niftiwrite(subtractedMask,outputFilePath,"Compressed",true);
end
Hope this helps!
Regards
Akshat Wadhwa