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