dicom pixel value matrix into CVS file

4 views (last 30 days)
I have 50 dicom files in one folder. i need to transfer pixel matrix of each image file into one common csv file and also the next image matrix must start from the next row in the same csv file. here is my code.
myFolder = ' ';
filePattern = fullfile(myFolder, '*.dcm');
image = dir(filePattern);
for k = 1:length(image)
baseFileName = image(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I= dicomread(fullFileName);
rowImage = reshape(I, 1, []);
if k == 1
csvwrite('file.csv', rowImage, 'delimeter',',');
else
csvwrite('file.csv', rowImage, 'delimiter',',','-append');
end
end
my code is not doing the thing i want . Anyone who can help with this code would be appreciated.
  13 Comments
Rik
Rik on 3 Sep 2020
You don't need to spread the things you want to say over separate comments.
If you know how to read a file and how to write a file, what steps are unclear to you?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Sep 2020
Writing each dcm file to a separate column:
myFolder = '403498/New';
outfile = fullfile(myFolder, 'file.csv');
filePattern = fullfile(myFolder, '*.dcm');
dcmimage = dir(filePattern);
numfiles = length(dcmimage);
images = nan(1, numfiles);
for k = 1:numfiles
baseFileName = dcmimage(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I = reshape(dicomread(fullFileName), [], 1);
numI = numel(I);
cursize = size(images,1);
if cursize < numI
images(end+1:numI,:) = nan;
elseif numI < cursize
I(end+1:cursize) = nan;
end
images(:,k) = I;
end
dlmwrite(outfile, images)
Because each file might be a different length, this pads with nan out to the longest file. It is possible to write out empty cells instead, but the file writing portion would be more work.
Note that it would be common for dcm files to have more than 1048576 elements, which is the maximum row limit for xlsx files. If you were planning and processing this csv file through excel or any tool uses excel to read csv files (such as xlsread()) then you are probably going to have problems.
I think you should be reconsidering writing the pixel values as text all into a common file.
  3 Comments
Rik
Rik on 7 Sep 2020
If it works, why not mark the answer as accepted? And please don't post comments in flags.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!