Extract column from text file

조회 수: 1 (최근 30일)
Suuz HMS
Suuz HMS 2023년 6월 12일
댓글: Suuz HMS 2023년 6월 13일
Hi, for my research project I have to analyze a certain output. The output gives a lot of measurements (also a lot of data I don't need), but I don't know how to extract the specific column of data that I do need. The data looks like this:
P1,R0,14:17:36.779,47,0.01421266,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0,0,0,-5.2,0
P0,R0,14:17:36.823,47,0.01403595,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0.08738,0.6293842,17.97661,-5.2,0
But I only need the first number between brackets (value -0.03077913 here). How do I do this?
  댓글 수: 2
the cyclist
the cyclist 2023년 6월 12일
The best method will depend on exactly what format of file you have. (For example, is it Excel, a CSV, etc.?) Can you upload the file (using the paper clip icon from the INSERT area of the toolbar), or at least a few representative lines?
Suuz HMS
Suuz HMS 2023년 6월 13일
The file gets downloaded as a txt-file, but can also be saved as a .m file.

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

답변 (2개)

Kautuk Raj
Kautuk Raj 2023년 6월 13일
To extract the first number between the brackets in the data provided, we can use regular expressions in MATLAB.
This regular expression pattern \[([-0-9\.]+);([-0-9\.]+);([-0-9\.]+)\] will match the three numbers between the brackets and captures them in separate groups.

Stephen23
Stephen23 2023년 6월 13일
Fake data (you would use FILEREAD):
S = sprintf('%s\n','P1,R0,14:17:36.779,47,0.01421266,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0,0,0,-5.2,0','P0,R0,14:17:36.823,47,0.01403595,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0.08738,0.6293842,17.97661,-5.2,0')
S =
'P1,R0,14:17:36.779,47,0.01421266,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0,0,0,-5.2,0 P0,R0,14:17:36.823,47,0.01403595,[-0.03077913; 0.3226232; -0.4286367],[0; 0; 0],[-0.4424625; 0.120887; -0.9627059],[-0.03836464; 0.5967162; 0.3170188],[-6.699989; -7.350024; 5.199996],50.48101,0.7456555,0.08738,0.6293842,17.97661,-5.2,0 '
C = regexp(S,'(?<=\[)(-|+)?\d+\.?\d*','match')
C = 1×10 cell array
{'-0.03077913'} {'0'} {'-0.4424625'} {'-0.03836464'} {'-6.699989'} {'-0.03077913'} {'0'} {'-0.4424625'} {'-0.03836464'} {'-6.699989'}
V = str2double(C)
V = 1×10
-0.0308 0 -0.4425 -0.0384 -6.7000 -0.0308 0 -0.4425 -0.0384 -6.7000

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by