load a large text file and split it after a specific character
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi community,
I am trying to load a large textfile with the following structure:
$
2019 12 382 5 2 0 59 27 58.1310 9.1815 28 30 10.8 -999.0 2 9 4 0 6399.9 642 0 7114
1.0 10.6077 28.2032 31.8321 1.0 11111
2.0 10.5912 28.2431 31.8602 2.0 11111
3.0 10.5605 28.2966 31.8909 3.0 11111
4.0 10.5092 28.3840 31.9397 4.0 11111
5.0 10.4638 28.4648 31.9863 5.0 11111
6.0 10.4423 28.4970 32.0023 5.9 11111
7.0 9.4124 29.9997 32.6715 6.9 11111
8.0 7.1264 32.7098 33.3106 7.9 11551
9.0 6.7706 32.6953 32.9885 8.9 11551
10.0 7.0900 32.7457 33.3127 9.9 11111
11.0 6.7539 33.1466 33.3850 10.9 11111
12.0 6.6182 33.2788 33.3859 11.9 11111
13.0 6.6485 33.3101 33.4414 12.9 11111
14.0 6.6804 33.3497 33.5058 13.9 11111
15.0 6.5948 33.4268 33.5004 14.9 11111
16.0 6.5790 33.4833 33.5379 15.9 11111
17.0 6.6125 33.5942 33.6681 16.8 11111
$
2019 12 383 5 2 4 59 52 58.1327 9.1823 28 31 9.4 -999.0 0 0 4 0 6407.3 642 0 7114
1.0 9.8245 29.8747 32.8847 1.0 11111
2.0 9.8204 29.8769 32.8840 2.0 11111
3.0 9.8186 29.8780 32.8840 3.0 11111
4.0 9.8207 29.8790 32.8871 4.0 11111
5.0 9.8238 29.8755 32.8866 5.0 11111
6.0 9.8226 29.8766 32.8871 5.9 11111
7.0 9.5859 30.1546 32.9674 6.9 11111
$
2019 12 384 5 2 9 0 33 58.1333 9.1833 4 7 8.4 -999.0 0 0 3 0 6414.5 642 0 7114
2.0 9.7569 29.9979 32.9517 2.0 11111
3.0 9.7065 30.0170 32.9294 3.0 11111
4.0 9.6149 30.0717 32.9083 4.0 11111
5.0 9.4743 30.1725 32.8918 5.0 11111
6.0 9.2935 30.3652 32.9313 5.9 11111
7.0 8.7304 30.9957 33.0726 6.9 11111
8.0 7.0311 32.8301 33.3378 7.9 11111
9.0 6.9079 32.8148 33.2168 8.9 11551
10.0 6.9923 32.8549 33.3275 9.9 11111
11.0 7.2111 32.8807 33.5429 10.9 11551
12.0 6.9807 32.8625 33.3252 11.9 11111
13.0 6.5475 33.2921 33.3362 12.9 11111
14.0 6.5317 33.3229 33.3505 13.9 11111
15.0 6.5289 33.3483 33.3714 14.9 11111
16.0 6.5656 33.3762 33.4294 15.9 11111
17.0 6.6680 33.4484 33.5855 16.8 11111
18.0 6.6379 33.5225 33.6263 17.8 11111
19.0 6.6291 33.5697 33.6616 18.8 11111
20.0 6.6351 33.6028 33.6973 19.8 11111
21.0 6.6318 33.6399 33.7283 20.8 11111
22.0 6.6495 33.7016 33.8003 21.8 11111
The first line contain some meta data (e.g. date time lat lon) and I want to store lat lon and the datetimes in a different cell.
After the "$" sign a new data set start consequently I want to store the data after the "$" sign in a new cell.
Is there any suggestion how I can wirte a load funtion to read the meta data in a different cell and save all data sets after the "$" sign in a separate cell?
Thank you very much in advance.
Regards, Jens
댓글 수: 0
답변 (1개)
Jan
2023년 2월 1일
편집: Jan
2023년 2월 2일
S = readlines('YourFile.txt');
ind = [find(startsWith(S, '$')); numel(S) + 1]; % [EDITED, readlines reply column vector]
n = numel(ind) - 1; % [EDITED, nBlock => n]
Meta = cell(1, n);
Data = cell(1, n);
for k = 1:n
c = ind(k); % [EDITED2]
Meta{k} = sscanf(S(c + 1), '%g');
Data{k} = sscanf(join(S(c + 2:ind(k+1) - 1)), '%g', [6, Inf]).';
end
댓글 수: 3
Jan
2023년 2월 1일
You are welcome. It is hard to test a code without having the input file. I've tried to fix the code, so please test it again.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!