Using regular expression to extract numeric data from text file

8 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Stephen23
Stephen23 il 3 Mar 2020
Modificato: Stephen23 il 3 Mar 2020

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by