How to Write file names of multiple images to a csv file.?

4 views (last 30 days)
From the code below,I got the file names of multiple images from a folder.
clear all
close all
clc
Directory = 'Images_folder\';
Imgs = [dir(fullfile(Directory,'*.bmp')); dir(fullfile(Directory,'*.jpg'));
dir(fullfile(Directory,'*.png'));dir(fullfile(Directory,'*.tif'))];
% D = dir('images');
ID = 1;MDMF=2;IDMF=3;HD=4;UHD=5;
m=[ID,MDMF,IDMF,HD,UHD];%comma separation is necessary for errorles result
for j=1:length(Imgs)
Img = imread(fullfile(Directory,Imgs(j).name)); % Read image
% figure
% imshow(Img)
end
headers={'File_Name',};
for i=1:length(Imgs)
f_name =Imgs(i).name;
f_name = f_name(1:end-4);
filename = sprintf('%s',f_name);
disp(filename)
csvwrite('Myfile.csv',filename)
end
My aim is to write these file names into first column of Myfile.csv.Bu I am getting error.Please help me

Answers (1)

Walter Roberson
Walter Roberson on 31 Oct 2017
You cannot use csvwrite() or dlmwrite() to selectively overwrite columns. You would need to use xlswrite() or writetable() to selectively overwrite columns.
If you are just trying to create a new file with those entries as the only content, then the easiest way is to do it "manually". No loop is required:
fid = fopen('Myfile.csv', 'wt');
[~, f_names] = cellfun(@fileparts, {Imgs.name}, 'uniform', 0);
fprintf(fid, '%s\n', f_names{:});
fclose(fid);

Tags

Community Treasure Hunt

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

Start Hunting!