How to use Loop to write multiple rows into my output CSV ?

I am writing a code to read multiple images from a path and analyze them to throw them into two buckets for ex: 'Good Images' and 'Bad Images'. I want to write the path of the image into an output CSV file which contains two columns : Path_GoodImage, Path_BadImage with their respective rows as the image paths.
Since i have a loop to read each image and analyze, i want too write the path under it's specific column for every image read.
How can i create a CSV file like such which takes the minimum time as i would be writing about a million rows!
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
I = imread(fullFileName);
% Some Analysis
If Image = Good
%%i want to write the path of the image which is fullFileName to the CSV under the column Path_GoodImage.
esle
%%i want to write the path of the image which is fullFileName to the CSV under the column Path_BadImage.
end
end
Also another question - Would it take more time if i actually just write the image using imwrite to two folders named 'GoodImage\' and 'BadImage\' . If Yes is it a significant amount of time difference.? I am scared to do this beacuse i am processing about a million images and time is a big concern.
Any help is appreciated. Thank You very much.

 Respuesta aceptada

Try it this way:
If Image == Good
%%i want to write the path of the image which is fullFileName to the CSV under the column Path_GoodImage.
% Write filename to column 3 and nothing to column 4.
fprintf(fid, '%d,%f,%s,,%f\n', value1, value2, fullFileName, value3);
else
%%i want to write the path of the image which is fullFileName to the CSV under the column Path_BadImage.
% Write nothing to column 3 and filename to column 4.
fprintf(fid, '%d,%f,,%s,%f\n', value1, value2, fullFileName, value3);
end
Take note of the commas in the format specifier strings in the above code. It's writing the fullFileName to either one column or the other.
It should not take any more time to write to two files than 1. Instead of fid which is the same for both cases, Just use fidGood for the file in the "good" folder, and fidBad for the file in the "bad" folder.

6 comentarios

Thank You Sir but i can't get it to work. I want the output CSV file to look like this :
Path_GoodImage Path_BadImage
C:Users\A\img1.tif C:Users\A\img4.tif
C:Users\A\img2.tif C:Users\A\img5.tif
C:Users\A\img3.tif C:Users\A\img7.tif
C:Users\A\img6.tif C:Users\A\img8.tif
. .
. .
. .
I think i should write the Header before the loop, is it ??
I tried this but doesn't work :
If Image == Good
fileID = fopen('ImageAnalysis.csv','w');
fprintf(fileID,'%s,%s\n','Path_Good','Path_Bad'); %%The Header's should probably be written before the loop ?!
fprintf(fileID,'%s\n', fullFileName);
fclose(fileID);
else
fileID = fopen('ImageAnalysis.csv','w');
fprintf(fileID,'%s,%s\n','Path_Good','Path_Bad');
fprintf(fileID, ' ,%s\n', ,fullFileName); %%I couldn't frame this line, i think it's wrong.
This is close but not as desired :
fileID = fopen('ImageAnalysis.csv','w');
fprintf(fileID,'%s,%s\n','Path_Good','Path_Bad');
If Image == Good
fprintf(fileID,'%s\n', fullFileName);
else
fprintf(fileID, ' ,%s\n', fullFileName);
end
fclose(fileID);
But the output CSV looks like this :
Path_GoodImage Path_BadImage
C:Users\A\img1.tif
C:Users\A\img2.tif
C:Users\A\img3.tif
C:Users\A\img3.tif
C:Users\A\img5.tif
C:Users\A\img6.tif
C:Users\A\img7.tif
C:Users\A\img8.tif
How can i eliminate those spaces ?? Plz help.
If you want no spaces, then you can't write the csv out a line at a time as you figure out if the image is good or bad. You're going to have to totally finish the loop and collect all the good images into one cell array, caGood, and all the bad ones into another cell array caBad, then do a second loop where you write them out on the same line.
% Second loop:
numGood = length(caGood);
numBad = length(caBad);
fprintf(fid, 'Path_GoodImage Path_BadImage\n');
for k = 1 : max([numGood, numBad])
if k <= caGood
goodString = sprintf('%s', caGood{k});
else
goodString = sprintf(' ');
end
if k <= caBad
badString = sprintf('%s', caGood{k});
else
badString = sprintf(' ');
end
% Now print both strings to the file.
fprintf(fid, '%s %s\n', goodString, badString);
end
Thank You Sir!
Can you please give me some info on this:
Would the string hold- say about a million records. ? Will this slow down the process or would there be any memory issues ?
No, the string holds one record, and you're writing out one string at a time. You could try building up one gigantic string with all the lines included in it before you write any out, and then just write out that enormous string in one call if you want. It might be faster - who knows?

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 3 de Ag. de 2015

Comentada:

el 4 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by