MATLAB: I want to save names of the images(frame_0000, frame_0001, frame_0003……..) in a text file by reading them from a folder.

text file

i want to save names of the images(frame_0000, frame_0001, frame_0003……..) in a text file by reading them from a folder. somehow i am getting errors while writing the code. As i am new to MatLab please help me. Waiting for your response.

Best Answer

D = 'directory where images are';
S = dir(fullfile(D,'*.jpg'));
% Save filenames:
[fid,msg] = fopen('names.txt','wt');
assert(fid>=3,msg)
fprintf(fid,'ImageName\n')
fprintf(fid,'%s\n',S.name)
fclose(fid);
% Load files:
for k = 1:numel(S)
Im = imread(fullfile(D,S(k).name));
...
end
This uses S.name to generate a comma-separated list, and so each filename gets printed to the textfile.