MATLAB: Converting txt file string to matrix.

MATLABmatrixmatrix arraystringstringstabletext file

I have an unhelpful txt file containing a string that looks something like:
{co-ordinates 1.5 2.5 4.8 weighting 11.7}{co-ordinates 2.5 2.8 1.7 weighting 21.4}{co-ordinates 1.5 2.5 4.8 weighting 11.7} …. etc
I'm trying to create a matrix with 4 collums (x co-ord, y co-ord, z co-ord, weighting) from this string. The co-ordinated are separated by a single space, the co-ords and weighting is separated by a double space.
Thanks in advance.

Best Answer

>> str = '{co-ordinates 1.5 2.5 4.8 weighting 11.7}{co-ordinates 2.5 2.8 1.7 weighting 21.4}{co-ordinates 1.5 2.5 4.8 weighting 11.7}';
>> mat = sscanf(str,'{co-ordinates%f%f%f weighting%f}',[4,Inf]).'
mat =
1.5 2.5 4.8 11.7
2.5 2.8 1.7 21.4
1.5 2.5 4.8 11.7
Related Question