Extracting numeric values from character array

10 visualizaciones (últimos 30 días)
MiauMiau
MiauMiau el 16 de Sept. de 2018
Comentada: MiauMiau el 27 de Sept. de 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

Respuesta aceptada

Stephen23
Stephen23 el 16 de Sept. de 2018
Editada: Stephen23 el 16 de Sept. de 2018
This is easy with sscanf:
>> 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
  7 comentarios
Stephen23
Stephen23 el 27 de Sept. de 2018
Editada: Stephen23 el 27 de Sept. de 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
MiauMiau el 27 de Sept. de 2018
Many thanks for all your time and help, that is really very helpful!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 16 de Sept. de 2018
T = regexp( regexprep(coordinates, '\[|\]', ''), 'split') ;
coords_num = str2double(T) ;
  3 comentarios
Walter Roberson
Walter Roberson el 16 de Sept. de 2018
Use cellstr(coordinates) where I had coordinates()
MiauMiau
MiauMiau el 17 de Sept. de 2018
Thank you - just for others to know: I tried using cellstr on coordinates, but that let to an empty array T. The answer of Stephen works however - many thanks to both

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by