MATLAB Answers

0

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

Asked by Renjith V Ravi on 31 Oct 2017
Latest activity Answered by Walter Roberson
on 31 Oct 2017
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

  0 Comments

Sign in to comment.

Tags

1 Answer

Walter Roberson 님의 답변 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);

  0 Comments

Sign in to comment.