Borrar filtros
Borrar filtros

Using regular expression to extract numeric data from text file

8 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Stephen23
Stephen23 el 3 de Mzo. de 2020
Editada: Stephen23 el 3 de Mzo. de 2020

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by