extracting matrices of numbers from a text file (txt) also containing words

1 Ansicht (letzte 30 Tage)
I have a txt file consisting of numbers and words as you can see in the attachment.
I have to create two matrices M1 and M2 with only the numbers (see figure). How can they be generated?
note: I have several such files. The columns of the two matrices are always the same (6 columns for M1 and 15 columns for M2); the rows are variable.
  2 Kommentare
Chuguang Pan
Chuguang Pan am 20 Mär. 2024
Bearbeitet: Chuguang Pan am 20 Mär. 2024
The readmatrix function can read the numbers. But it returns a big matrix, which contain both M1 and M2 as submatrix.
A=readmatrix("test.txt")

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 20 Mär. 2024
f='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1646741/test.txt';
m=readmatrix(f);
whos m
Name Size Bytes Class Attributes m 41x15 4920 double
[m(1:5,:);m(end-5:end,:)]
ans = 11×15
1.0000 0 0 -26.4755 -179.5206 -223.9298 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.0000 0 0 -26.6554 -179.5778 -223.9270 NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.0000 0 0 -26.7568 -179.6101 -223.9253 NaN NaN NaN NaN NaN NaN NaN NaN NaN 4.0000 0 0 -26.8583 -179.6423 -223.9237 NaN NaN NaN NaN NaN NaN NaN NaN NaN 5.0000 0 0 -26.3368 -179.4678 -223.9235 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
shows that readmatrix can find numeric data but as the other poster notes, isn't all that clean as towards your needed/intended result.
m=m(~all(m,2),:); % remove rows that aren't all NaN and then see what have left
whos m
Name Size Bytes Class Attributes m 19x15 2280 double
[m(end-9:end,:)]
ans = 10×15
10.0000 0 0 -26.1032 -179.3438 -223.8784 NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 1.0000 208.0000 207.0000 216.0000 216.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 2.0000 103.0000 96.0000 97.0000 97.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 3.0000 61.0000 59.0000 60.0000 60.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 4.0000 59.0000 56.0000 57.0000 57.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 5.0000 39.0000 36.0000 38.0000 38.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 6.0000 26.0000 22.0000 23.0000 23.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 7.0000 18.0000 17.0000 20.0000 20.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 8.0000 437.0000 436.0000 438.0000 438.0000 1.0000 1.0000 1.0000 0 0 0 0 0 4.0000 0 9.0000 7.0000 4.0000 6.0000 6.0000
ix2=all(isfinite(m),2);
m2=m(ix2,:)
m2 = 9×15
1 1 1 0 0 0 0 0 4 0 1 208 207 216 216 1 1 1 0 0 0 0 0 4 0 2 103 96 97 97 1 1 1 0 0 0 0 0 4 0 3 61 59 60 60 1 1 1 0 0 0 0 0 4 0 4 59 56 57 57 1 1 1 0 0 0 0 0 4 0 5 39 36 38 38 1 1 1 0 0 0 0 0 4 0 6 26 22 23 23 1 1 1 0 0 0 0 0 4 0 7 18 17 20 20 1 1 1 0 0 0 0 0 4 0 8 437 436 438 438 1 1 1 0 0 0 0 0 4 0 9 7 4 6 6
m1=m(~ix2,:); m1=m1(:,all(isfinite(m1)))
m1 = 10×6
1.0000 0 0 -26.4755 -179.5206 -223.9298 2.0000 0 0 -26.6554 -179.5778 -223.9270 3.0000 0 0 -26.7568 -179.6101 -223.9253 4.0000 0 0 -26.8583 -179.6423 -223.9237 5.0000 0 0 -26.3368 -179.4678 -223.9235 6.0000 0 0 -26.9245 -179.6633 -223.9227 7.0000 0 0 -26.9567 -179.6734 -223.9220 8.0000 0 0 -27.0561 -179.7044 -223.9199 9.0000 0 0 -27.2294 -179.7586 -223.9162 10.0000 0 0 -26.1032 -179.3438 -223.8784
Alternatively, you could parse the file as text looking for the FORTRAN format string just ahead of each array and the -1 in the record past each array and then read/convert those sections directly.
Depending upon how large real files might be, would be interesting to see the performance difference between the explicit conversion and then the builtin parsing inside the readmatrix routine.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by