how to combine a lot of file into one file acording specific file name?
1 view (last 30 days)
Show older comments
I have a lot of file like this
File 1 : Jason_Data_J1p0012_98.txt
> cat Jason_Data_J1p0012_98.txt
98 0 87 99 0.98 ...
File 2 : Jason_Data_J1p0012_77.txt
> cat Jason_Data_J1p0012_77.txt
97 12 45 45 0.11 ...
File 3 : Jason_Data_J1p0001_90.txt
File 4 : Jason_Data_J1p0001_8.txt
.. and more
I want the file to divide into new file acording to the name of J1p*_
So like this
File 1 : Combine_J1p0012.txt
> cat Combine_J1p0012.txt
98 0 87 99 0.98 ...
97 12 45 45 0.11 ...
and File 2 : Combine_J1p0001.txt.
I have tried like this, but the result of the group has a same value.
clc;
clear;
format short;
Y = dir('D:\Reformat new file\done\Jason_Data_J1p*.txt');
for B=1:length(Y);
H = Y(B).name;
gg = H(:,12:18);
fid_p = fopen(['Combine_' gg '.txt'],'w'); % writing file id
x = Y;
for i =1:length(x)
D=x(i).name;
filename = x(i).name; %filename
fid_t=fopen(filename,'r');%open it and pass id to fscanf (reading file id)
data = fscanf(fid_t,'%c');%read data
fprintf(fid_p,'%c',data);%print data in fid_p
fclose(fid_t);% close reading file id
fprintf(fid_p,'\n');%add newline
end
fclose(fid_p); %close writing file id
end
Anyone has a solution to this? Thank you.
1 Comment
Image Analyst
on 15 Sep 2021
Attach at least two of your files so people have something to work with, after you read this link:
Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!