combining two text file with 100 equal number of header and body text

I am beginner for using matlab. I have two files each with 100 events formatted like below. Then I would like to combine them under each of the header in one file .
181123 1020 36.76 56.8409N 99.9979E 10.89 0.00 123 0.22
LS14RT 10.54LS13RT 11.84LS12RT 12.54LS11RT 12.54LS15RT 12.94LS16RT 14.54
LS67RT 15.34LS17RT 16.94LS45RT 18.84LS18RT 19.34LS44RT 20.04LS19RT 20.94
LS63RT 21.14CCGMRT 21.84LS22RT 23.04LS66RT 24.94LS39RT 25.14LS34RT 26.44
181123 1020 36.76 56.8409N 99.9979E 10.89 0.00 123 0.22
LS14RT 18.34LS13BT 20.64LS12BT 21.74LS11BT 22.04LS15BT 22.64LS16BT 25.04
LS67BT 26.24LS17BT 29.14LS45BT 32.44LS18BT 33.54LS44BT 34.34LS19BT 35.54
Like this. Is there any help on this please?
181123 1020 36.76 56.8409N 99.9979E 10.89 0.00 123 0.22
LS14RT 10.54LS13RT 11.84LS12RT 12.54LS11RT 12.54LS15RT 12.94LS16RT 14.54
LS67RT 15.34LS17RT 16.94LS45RT 18.84LS18RT 19.34LS44RT 20.04LS19RT 20.94
LS63RT 21.14CCGMRT 21.84LS22RT 23.04LS66RT 24.94LS39RT 25.14LS34RT 26.44
LS14RT 18.34LS13BT 20.64LS12BT 21.74LS11BT 22.04LS15BT 22.64LS16BT 25.04
LS67BT 26.24LS17BT 29.14LS45BT 32.44LS18BT 33.54LS44BT 34.34LS19BT 35.54

6 commentaires

What is it that occurs 100 times? One header line and then 100 entries similar to the LS* entries? Or 100 groups of (one header line followed by a varying number of LS* entries) ?
There are two files. One has 100 headerlines with 100 LS14RT 18.34... and another one has also 100 headerlines with 100 LS67BT 26.24...
Then they will be combined under each 100 headerlines of one file.
So that would be like
numeric header line #1
LS line #1-1
LS line #1-2
...
LS line #1-100
numeric header line #2
LS line #2-1
LS line #2-2
...
numeric header line #100
LS line #100-1
LS line #100-2
..
LS line #100-100
for a total of 100*(101) = 10100 lines per file, possibly more due to blank lines?
Thank you for the question. But sorry for bad explantion. This is the 3 of 100 events. It looks like this. In order to be clear I made text shorter than before. But structure is same. The bold ones are the header which I would like to take and then I would like to add lines like LS....
181123 1020 36.76 56.8409N 99.9979E 10.89
LS14RT 18.34LS13RT 20.64LS12RT 21.74
TS67RT 26.24LS17RT 29.14LS45RT 32.44
181229 1736 58.47 29.3831N 109.3904E 11.20
LS67RT 8.29LS25RT 19.09LS46RT 20.29
181311 032 49.72 36.7392N 119.2560E 6.86
LS25RT 9.56LS67RT 15.06LS26RT 17.76
LS66RT 25.97LS27RT 25.06LS47RT 26.76
LS44RT 34.97LS13RT 36.66LS10RT 36.56
The easiest way to make it completely clear is to attach two example text files to your question. It will also help answer what line ending and character encoding is used.
Thanks for the idea. I have uploaded the input file called file1.txt and file2.txt. Also the output which I want to create.

Connectez-vous pour commenter.

 Réponse acceptée

z = dir('file*_.txt');
zn = {z.name};
n = numel(zn);
c = cell(n,1);
for jj = 1:numel(zn)
f1 = fopen(zn{jj});
k = textscan(f1,'%s','delimiter','\n');
fclose(f1);
c{jj} = k{:};
end
c = cat(1,c{:});
H = regexp(c,'^\d+.*','match','once');
lo = ~cellfun(@isempty,H);
[a,b,c1] = unique(H(lo),'stable');
O1 = accumarray(c1(cumsum(lo)),(1:numel(c))',[],@(x){c(sort(x))});
for jj = 1:numel(O1)
lo1 = ~cellfun(@isempty,O1{jj});
lo2 = circshift(lo1,1) & lo1;
lo2([1,end]) = true;
O1{jj} = O1{jj}(lo2);
end
out = cat(1,O1{:});

2 commentaires

Thank you for the answer. Sorry, I am complete beginner. I don not understand why we have only one input here. Is it input or ouput (z = dir('file*_.txt');)? I tried with two input files assuming this one is output. Could you please show me the complete script with the output? Thanks again.
The first line with the dir call looks for all .txt files in the current directory whose name starts with file, followed by anything, followed by _.txt . The first for loop then reads them all in.
It looks to me as if after this code, the variable out will contain a cell array of character vectors, each entry representing one line, with the lines already in the order that you would want to output them. The only thing I see missing is an empty line before each header.
From here I suspect you would
fid = fopen('new_file.txt', 'wt');
fwrite(fid, '%s\n', out{:});
fclose(fid)

Connectez-vous pour commenter.

Plus de réponses (1)

Bujee
Bujee le 25 Avr 2019
Thanks a lot. It worked. But there is leaving a little problem in my combined text. when last line is shorter than others in the input files there is space. I would put exactly after the text when I combine them. I mean space must be removed between combined texts. Thank you so much.

Community Treasure Hunt

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

Start Hunting!

Translated by