Asked by MiauMiau
on 16 Sep 2018

Hi, I have a character array "coords" (containing 3D coordinates), which looks something like this:

val =

'[ 48. -24. 48.]'

'[ 50. -22. 58.]'

'[ 48. -20. 50.]'

I would like to find the index of a specific coordinate, something like:

find(coords == [50 -22 58])

How do I do that? Transforming coords as follows:

coords_num = str2double(coordinates)

just leads to:

coords_num = NaN

Answer by Stephen Cobeldick
on 16 Sep 2018

Edited by Stephen Cobeldick
on 16 Sep 2018

Accepted Answer

>> coords = ['[ 48. -24. 48.]';'[ 50. -22. 58.]';'[ 48. -20. 50.]';'[ 49. -23. 52.]']

coords =

[ 48. -24. 48.]

[ 50. -22. 58.]

[ 48. -20. 50.]

[ 49. -23. 52.]

>> mat = reshape(sscanf(coords.','[%f%f%f]'),3,[]).' % convert to numeric

mat =

48 -24 48

50 -22 58

48 -20 50

49 -23 52

>> idx = all(mat==[50,-22,58],2) % match row

idx =

0

1

0

0

>> find(idx)

ans = 2

MiauMiau
on 27 Sep 2018

Stephen Cobeldick
on 27 Sep 2018

"Not exactly a bug since these are euclidean coordinates and in this line the y coordinates has only one digit. "

Nowhere in my comment did I say that the bug is due to the number of digits. As I wrote, the bug is due to the trailing space characters, which means that this line is formatted differently to all other rows of that char array. It is a bug in that data because that row is formatted differently to all of the others: it has trailing space characters, which none of the other rows do. Nothing to do with digits at all. For example, this works perfectly:

[ 24. -30. 58.]

[ 58. -4. 34.]

[ 28. -24. 74.]

proving that the bug has nothing to do with how many digits any number has.

If you look at that row carefully and compare with the other rows, you will see that each of the three numbers is missing exactly one leading space character. If you add in each of those missing leading space characters, you get numbers that align with all of the other rows and without any trailing spaces required. So there is definitely something buggy about that row!

"what do you mean by your second comment?"

Do you mean my second suggestion for how to deal with that bug in your data? Well, I thought that my code would make that reasonably clear, but here is a step-by-step explanation of my suggestion:

- "remove trailing space characters. This will probably require reshaping to a char vector:"

Lets have a look at just the rows 283 to 285:

>> tmp = coords(283:285,:)

tmp =

[ 24. -30. 58.]

[58. -4. 34.]

[ 28. -24. 74.]

Check that row yourself: does it have three trailing space characters? Answer: yes it does. Now lets reshape those three rows into a vector:

>> vec = reshape(tmp.',1,[])

vec =

[ 24. -30. 58.][58. -4. 34.] [ 28. -24. 74.]

^^^ Oh no! The trailing space characters!

How can we deal with those pesky trailing space characters? How about we get rid of them? How could we do that, considering that we don't know where they are, or how many there might be? A regular expression would lets us do that. See my previous comment to see one way to do that.

MiauMiau
on 27 Sep 2018

Many thanks for all your time and help, that is really very helpful!

Sign in to comment.

Answer by Walter Roberson
on 16 Sep 2018

T = regexp( regexprep(coordinates, '\[|\]', ''), 'split') ;

coords_num = str2double(T) ;

MiauMiau
on 16 Sep 2018

Thank you very much, but I get this error:

T = regexp( regexprep(coordinates, '\[|\]', ''), 'split') ;

Error using regexprep

The 'STRING' input must be either a char row vector, a cell array of char row vectors, or

a string array.

Why is that? If its of any help, it says "2092 x 16 char" (2092 coordinates stored).

Walter Roberson
on 16 Sep 2018

Use cellstr(coordinates) where I had coordinates()

MiauMiau
on 17 Sep 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Paolo (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/419263-extracting-numeric-values-from-character-array#comment_611077

Sign in to comment.