Using regular expression to extract numeric data from text file

8 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2020-3-3
编辑:Stephen23 2020-3-3

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by