Extract column from text file
1 view (last 30 days)
Show older comments
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 Comments
the cyclist
on 12 Jun 2023
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?
Answers (2)
Kautuk Raj
on 13 Jun 2023
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.
0 Comments
Stephen23
on 13 Jun 2023
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')
C = regexp(S,'(?<=\[)(-|+)?\d+\.?\d*','match')
V = str2double(C)
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!