Clear Filters
Clear Filters

How to read multi .txt files and extract files which are started with the specific name and write all these files in a new .txt file?

2 views (last 30 days)
I have around 10000 .dat files. Their names are like this :
tag-klima-500-1987-1_12.dat ; tag-klima-500-1987-1_12.dat ; tag-klima-905-1987-1_12.dat ; tag-klima-905-1987-1_12.dat
I need to read those files with the same middle number, for example, all those that have 500 , or 905 in the middle. And then write all data belong to each group (groups which have identification of 500 or 905) in a .txt file.
I could read and write for files belong to each group, for example for files starting with tag-klima-905, but I have stuck to do it for all file at the same time when running the codes.
pth = 'E:\DATA\Agro\ZAMG\klima-ehsan-1987_2017';
fileList = dir(fullfile(pth, 'tag-klima-905*.dat'));
out = fopen(fullfile(pth, 's905.txt'), 'w');
delimiter = '\t';
startRow = 2;
formatSpec = '%s%s%s%s%s%s%s%[^\n\r]';
for k = 1:numel(fileList)
s = fileread(fullfile(pth, fileList(k).name));
fwrite(out, s, 'char');
end
fclose(out);
I appreciate any helps,

Answers (0)

Community Treasure Hunt

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

Start Hunting!