How to change patient name?
6 views (last 30 days)
Show older comments
mohd akmal masud
on 27 Sep 2021
Commented: Walter Roberson
on 28 Sep 2021
Hi all,
I have 263 images dicom. But I want to change the patient name every images. I wrote this code but failed.
P = zeros(256, 256, 263);
for K = 263 : -1 : 1
petname = sprintf('%03d.dcm', K);
info(K) = dicominfo(petname);
P(:,:,K) = dicomread(petname);
end
newname = 'example'
dicomwrite(P, 'Q.dcm', newname, info)
but failed
Anyone can help me?
2 Comments
Image Analyst
on 27 Sep 2021
What failed? You should know by now that you need to show us ALL the red text. I have no idea if dicominfo() failed because a file did not exist, or if dicomwrite failed for some reason, or if the insertion of the array into the k'th slice of P failed (maybe because the size is wrong).
Accepted Answer
Walter Roberson
on 27 Sep 2021
P = zeros(256, 256, 263);
for K = 263 : -1 : 1
petname = sprintf('%03d.dcm', K);
info(K) = dicominfo(petname);
So info will end up being a non-scalar struct array. (There is also a risk of problems as not all dicom images have the same field names.)
P(:,:,K) = dicomread(petname);
You are storing data for all of the files into one array.
end
newname = 'example'
dicomwrite(P, 'Q.dcm', newname, info)
You are trying to use the non-scalar struct array info, but the metadata must be a scalar struct array, as far as I know.
The syntax you used for dicomwrite() with four parameters, would have to match one of
dicomwrite(X,cmap,filename,meta_struct)
dicomwrite(X,cmap,filename,info)
dicomwrite(X,filename,NAME,VALUE)
but cmap cannot be a character vector such as 'Q.dcm', so the first two are ruled out. So we have to check: is the variable newname a character vector or string? Yes, it is. So the third form must be the interpretation. So would be trying to look for a dicomwrite option named example and info would be the value associated with it. Which is not going to work.
Perhaps you were thinking of setting the PatientName fields
[info.PatientName] = deal(struct('FamilyName', newname, 'GivenName', newname));
and then doing
dicomwrite(P, 'Q.dcm', info)
But... (A) I do not know if you can use non-scalar info; and (B) the info you have is for a 2D image, but P is a 3D array, which is a miss-match.
8 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!