How to split text files into many text files?

조회 수: 4 (최근 30일)
hanif hamden
hanif hamden 2020년 8월 25일
댓글: hanif hamden 2020년 8월 25일
Hi everyone. Can anyone help us to split the data into many files. The example data is attached.
This is how the data look like..
Lat: 6.4931, Lon: 99.6067, Parameter: z(m) <--This is the start line
Depth (m): 22.45
Constituents included: m2 s2 k1 o1
Time start: 00:00, 1. 1.1993
Time step (min): 60.00
Time Series length (hours):236664
01-Jan-1993 00:00:00 -0.0946
01-Jan-1993 01:00:00 -0.3369
01-Jan-1993 02:00:00 -0.5110
... More Data
31-Dec-1993 23:59:59 -1.1673 <-- This is the last line
Lat: 6.3933, Lon: 99.6067, Parameter: z(m) <--- Another start line for different coordinate
Depth (m): 30.74
Constituents included: m2 s2 k1 o1
Time start: 00:00, 1. 1.1993
Time step (min): 60.00
Time Series length (hours):236664
01-Jan-1993 00:00:00 -0.1017
01-Jan-1993 01:00:00 -0.3324
01-Jan-1993 02:00:00 -0.4965
... More Data
31-Dec-1993 23:59:59 -1.1284 <--Another last line

채택된 답변

Rik
Rik 2020년 8월 25일
편집: Rik 2020년 8월 25일
The code below assumes each file ends with an empty line. You could also search for 'Lat:' instead to determine the beginning of a new file.
You can get the readfile function from the FEX or through the AddOn-manager (R2017a or later).
data=readfile('https://www.mathworks.com/matlabcentral/answers/uploaded_files/351353/Example.txt');
lastlineoffile=find(cellfun('isempty',data));
lastlineoffile=[0;unique([lastlineoffile;numel(data)])];%add last line just to make sure
for n=1:(numel(lastlineoffile)-1)
currentfile=data((lastlineoffile(n)+1):lastlineoffile(n+1));
fid=fopen(sprintf('aa__file%03d.txt',n),'w');
fprintf(fid,'%s\n',currentfile{:});
fclose(fid);
end
  댓글 수: 3
Rik
Rik 2020년 8월 25일
You're welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by