Using regular expression to extract numeric data from text file

16 views (last 30 days)
Hi, I have a text file containing a bunch of lines with the following format:
"Triangle(Vector3D(-0.125, -0.375, 0), Vector3D(0.125, -0.375, 0.166396), Vector3D(0.125, -0.375, 0))"
I would like to extract the numeric values contained within the parentheses.
Right now I am attempting to make a matrix Mx9, where M is the number of lines in the text file, and the 9 comes from the 3 elements of the three vectors. I try doing this using a regular expression and capture groups:
string = fileread('triangle_positions.txt')
expression = '(-?\d+\.?\d*),\s(-?\d+\.?\d*),\s(-?\d+\.?\d*)'
tokens = regexp(string, expression, 'tokens');
The result is a "cell" containing the values as strings. For a single line I get a "1x3 cell" where each cell contains the coordinates of the vector as strings.
How would you convert this into a regular matlab matrix?
Thanks in advance
Søren

Accepted Answer

Stephen23
Stephen23 on 3 Mar 2020
Edited: Stephen23 on 3 Mar 2020

More Answers (0)

Categories

Find more on Data Type Conversion 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!