MATLAB: Copy 1 part of text in complex text file

MATLABtext file

I have one complex text file named "output.txt"
.
.
.
~~~ output final hypocenters ...
date origin latitude longitude depth mag no rms x y z
1 960415 0839 55.21 21.5634N 105.7475E 19.88 0.00 12 0.982 -227.88 -35.17 19.88
.
.
.
1837 180326 0439 56.97 21.5877N 103.9346E 21.37 0.00 11 1.062 -40.30 -32.47 21.37
~~~ output final station residuals ...
.
.
.
and i need to copy the part on this file into "final_hypos.out" file
date origin latitude longitude depth mag no rms x y z
1 960415 0839 55.21 21.5634N 105.7475E 19.88 0.00 12 0.982 -227.88 -35.17 19.88
.
.
.
1837 180326 0439 56.97 21.5877N 103.9346E 21.37 0.00 11 1.062 -40.30 -32.47 21.37
the string to detect the data " ~~~ output final hypocenters …" and " ~~~ output final station residuals …" only one time in file.
Any way to do this. I tried several way but it not success.
I attach the file here also
Thanks in advance

Best Answer

This copies the exact text from the input file into the output file, starting from "output final hypocenters" until the end of that block of data. The input and output files are attached to this answer:
ofh = '~~~ output final hypocenters';
[fi1,msg] = fopen('output.txt','rt');
[fi2,msg] = fopen('final_hypos.out','wt');
assert(fi1>=3,msg)
assert(fi2>=3,msg)
str = '';
while ~strncmp(str,ofh,numel(ofh))
str = strtrim(fgetl(fi1));
end
str = '';
while isempty(strtrim(str))
str = fgetl(fi1);
end
while ~isempty(strtrim(str)) && ~strncmp(strtrim(str),'~~~',3)
fprintf(fi2,'%s\n',str);
str = fgetl(fi1);
end
fclose(fi1);
fclose(fi2);
The output file contains this:
date origin latitude longitude depth mag no rms x y z
1 960415 0839 55.21 21.5634N 105.7475E 19.88 0.00 12 0.982 -227.88 -35.17 19.88
2 960622 1839 26.78 21.2159N 103.2645E 8.06 0.00 7 9.627 29.05 -73.64 8.06
3 960928 1314 14.53 19.4704N 102.8734E 13.75 0.00 11 6.272 69.97-266.92 13.75
4 961221 1751 4.82 20.4105N 100.2162E 8.16 0.00 316.790 345.75-162.82 8.16
5 970702 1557 1.99 20.9215N 102.6076E 9.98 0.00 8 5.114 97.20-106.25 9.98
6 971024 0930 40.25 22.5243N 101.6269E 9.57 0.00 7 8.966 197.79 71.23 9.57
7 971119 0651 53.92 22.0071N 101.7117E 10.00 0.00 412.446 189.38 13.96 10.00
8 980210 0818 3.39 21.1240N 104.8061E 15.83 0.00 14 1.284 -130.67 -83.82 15.83
9 980213 1458 5.06 23.6673N 105.8520E 18.35 0.00 11 3.341 -236.92 197.80 18.35
10 980213 2315 0.21 23.9868N 102.7740E 8.17 0.00 4 7.048 79.09 233.18 8.17
11 980514 0334 44.95 20.9545N 103.3424E 9.36 0.00 13 5.413 21.00-102.59 9.36
... lots of lines here
832 180220 1409 56.30 24.5814N 101.0812E 10.00 0.00 810.252 252.18 299.01 10.00
833 180228 1723 38.50 23.0752N 102.9323E 18.14 0.00 8 4.553 63.06 132.23 18.14
834 180312 2008 32.26 19.9176N 105.2585E 15.63 0.00 15 2.393 -178.27-217.41 15.63
835 180313 0447 46.58 22.5116N 103.9298E 8.69 0.00 8 0.751 -39.68 69.82 8.69
836 180318 2053 54.36 21.7291N 104.2485E 8.31 0.00 10 0.617 -72.74 -16.82 8.31
837 180326 0439 56.97 21.5877N 103.9346E 21.37 0.00 11 1.062 -40.30 -32.47 21.37