MATLAB: Error with dicomwrite: Attribute has wrong data type.

anonymizationdicomwrite

Hello! I am trying to anonymize dicom files by loading the original file with dicominfo and dicomread, changing some fields, and then writing the new file. However, I encountered the error below:
Error using dicom_add_attr>validate_data (line 118)
Attribute (7FE0,0010) has wrong data type.
Error in dicom_add_attr (line 103)
attr_str(pos).Data = validate_data(data, attr_str(pos), specificCharacterSet);
Error in dicom_convert_meta_to_attr (line 39)
attr = dicom_add_attr([], tag(1), tag(2), dictionary, specificCharacterSet, data, VR);
Error in dicom_convert_meta_to_attr>encode_item (line 151)
new_attr = dicom_convert_meta_to_attr(attr_names{p}, item_struct, dictionary, txfr, specificCharacterSet);
Error in dicom_convert_meta_to_attr>encode_SQ (line 125)
data = encode_item(SQ_struct.(items{p}), dictionary, txfr, specificCharacterSet);
Error in dicom_convert_meta_to_attr (line 26)
data = encode_SQ(metadata.(attr_name), dictionary, txfr, specificCharacterSet);
Error in dicom_copy_IOD (line 49)
new_attr = dicom_convert_meta_to_attr(attr_name, metadata, dictionary, options.txfr, specificCharacterSet);
Error in dicomwrite>write_message (line 277)
[attrs, status] = dicom_copy_IOD(X, map, …
Error in dicomwrite (line 208)
[status, options] = write_message(X, filename, map, metadata, options);
I have also tried simply reading in the original file and writing out the new file (without changing any fields) –
>> y = dicomread('original.dcm');
>> x = dicominfo('original.dcm');
>> dicomwrite(y,'new.dcm',x,'WritePrivate',true,'CreateMode','Copy');
– but these actions produced the same error, as did using dicomanon. Any guidance would be much appreciated!

Best Answer

>> y = dicomread('original.dcm');
>> x = dicominfo('original.dcm');
>> x.PatientName = 'anon'; %you'll need to do something like this for every header field that you want to anonymize
>> x.IconImageSequence = []; % this is the key line for avoiding the attribute error
>> dicomwrite(y,'new.dcm',x,'WritePrivate',true,'CreateMode','Copy');