Only read number between brackets []

10 Ansichten (letzte 30 Tage)
Olayne
Olayne am 8 Apr. 2022
Kommentiert: Olayne am 8 Apr. 2022
Hello,
I have a camera that records events on every frame as below. There could be thousands of frames in a single text ('clog') file, and multiple events per frame (at least one).
I am looking to only read the four numbers between the square braquets and write them into a matrix with four columns, each row being the immediate next set (could be to the right or below)
I have tried textscan and alldata unsucessfully.
Frame 1 (409804.687500, 0.000000 s)
[242, 83, 18.0403, 0]
[64, 79, 17.4317, 151.5625]
Frame 2 (412404.687500, 0.000000 s)
[154, 106, 22.1719, 0] [154, 107, 8.08874, 17.1875]
[188, 73, 19.039, 176.5625]
Frame 3 (423560.937500, 0.000000 s)
[36, 59, 17.4567, 0] [35, 59, 11.429, 10.9375]
[21, 248, 11.3223, 156.25] [22, 248, 9.13357, 159.375]
Frame 4 (426721.875000, 0.000000 s)
[237, 28, 30.0476, 0] [237, 29, 16.3758, 1.5625]
[135, 168, 5.45964, 117.1875]
Frame 5 (430692.187500, 0.000000 s)
[241, 144, 43.3381, 0]
[29, 206, 22.4948, 131.25]
Frame 6 (431220.312500, 0.000000 s)
[32, 202, 17.4553, 0] [32, 203, 8.08062, 15.625] [31, 202, 7.46939, 26.5625]
[69, 227, 17.182, 137.5] [70, 227, 7.13842, 159.375]
Frame 7 (434304.687500, 0.000000 s)
[46, 85, 22.0349, 0] [46, 86, 16.5734, 1.5625]
[125, 251, 20.5508, 118.75] [126, 251, 7.23025, 134.375]
Frame 8 (436587.500000, 0.000000 s)
[167, 150, 19.2099, 0]
[81, 242, 17.4231, 151.5625]
  2 Kommentare
Image Analyst
Image Analyst am 8 Apr. 2022
There seems to be an inconsistency between lines. Some lines have one set of bracketed numbers, while others have 2 or 3. This inconsistency may be why traditional functions don't work. Can you attach your original text file? Do you just want to put each set into one row of the matrix as you encounter them?
Olayne
Olayne am 8 Apr. 2022
Bearbeitet: Olayne am 8 Apr. 2022
I have attached the original (truncated to below 5MB) file, however please note that the original extension is '.clog'. I indeed want to put each set of 4 numbers in a row as they are encountered. If that is not possible, then rows can be maintained (say if there is three sets on one raw of the text file, this could give 12 columns).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 8 Apr. 2022
Try this:
str = fileread('Trial1.txt');
openingBracketLocations = strfind(str, '[')
openingBracketLocations = 1×120238
38 62 131 154 185 254 275 303 330 399 421 450 521 546 614 636 663 693 718 787 808 836 864 934 959 1029 1051 1080 1151 1173
closingBracketLocations = strfind(str, ']')
closingBracketLocations = 1×120238
58 88 152 181 211 273 299 328 356 419 446 478 542 571 634 661 689 716 744 806 832 862 891 955 986 1049 1076 1107 1169 1196
% Check for any missing brackets.
any((closingBracketLocations - openingBracketLocations) < 0) % Should be 0
ans = logical
0
% Create output matrix
m = zeros(length(closingBracketLocations), 4);
for row = 1 : size(m, 1)
thisSet = str(openingBracketLocations(row) + 1 : closingBracketLocations(row) - 1);
numbers = cell2mat(textscan(thisSet, '%f,'));
m(row, :) = numbers;
end
% Show in command window.
m
m = 120238×4
242.0000 83.0000 18.0403 0 64.0000 79.0000 17.4317 151.5625 154.0000 106.0000 22.1719 0 154.0000 107.0000 8.0887 17.1875 188.0000 73.0000 19.0390 176.5625 36.0000 59.0000 17.4567 0 35.0000 59.0000 11.4290 10.9375 21.0000 248.0000 11.3223 156.2500 22.0000 248.0000 9.1336 159.3750 237.0000 28.0000 30.0476 0
  1 Kommentar
Olayne
Olayne am 8 Apr. 2022
That works perfectly. Thank you for your quick answer, that is extremely helpful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by