dicom pixel value matrix into CVS file
Show older comments
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
on 27 Aug 2020
This code looks like it should work. How is it not doing what you want?
Note that images tend to be very large when converted to text and might not actually fit on a single line of a text file.
Also, why do you want to do this?
Walter Roberson
on 28 Aug 2020
Code looks okay, but for efficiency I would recommend opening the file for writing once, and then using fprintf() statements.
fid = fopen('file.csv', 'wt');
[...]
%then instead of csvwrite...
fprintf(fid, '%g,', rowImage(1:end-1));
fprintf(fid, '%g\n', rowImage(end));
Eventually
fclose(fid)
Rik
on 31 Aug 2020
What editor are you using to see the result of Walters code? If you're using notepad or a similar program: some older programs don't recognize \n as a line ending and require \r to be present as well. Newer/smarter programs like Notepad++ and most programs on non-Windows machines will show a line end with only \n.
Walter Roberson
on 31 Aug 2020
However the 'wt' option I gave in my suggestion would take care of putting in cr and lf if run on Windows. (There could still be an issue if the code was run on Mac or Linux and the file was copied to Windows)
Walter Roberson
on 31 Aug 2020
Worked fine in my test.
myFolder = '403498/New';
outfile = fullfile(myFolder, 'file.csv');
filePattern = fullfile(myFolder, '*.dcm');
dcmimage = dir(filePattern);
fid = fopen(outfile, 'wt');
for k = 1:length(dcmimage)
baseFileName = dcmimage(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I= dicomread(fullFileName);
rowImage = reshape(I, 1, []);
fprintf(fid, '%g,', rowImage(1:end-1));
fprintf(fid, '%g\n', rowImage(end));
end
fclose(fid);
Rik
on 3 Sep 2020
You will have to read the entire file and write it with the extra column.
Walter Roberson
on 3 Sep 2020
Text files can only do that by being rewritten in full.
xlsx are really text files underneath by the way. But it is an easier interface to use them.
Perhaps it would be practical to put each file as a separate worksheet of xlsx file?
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?
Deepa S
on 7 Sep 2020
Accepted Answer
More Answers (0)
Categories
Find more on Use COM Objects in MATLAB 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!