Extracting numeric values from character array
12 views (last 30 days)
Show older comments
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
Accepted Answer
Stephen23
on 16 Sep 2018
Edited: Stephen23
on 16 Sep 2018
>> 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 Comments
Stephen23
on 27 Sep 2018
Edited: Stephen23
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.
More Answers (1)
Walter Roberson
on 16 Sep 2018
T = regexp( regexprep(coordinates, '\[|\]', ''), 'split') ;
coords_num = str2double(T) ;
3 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!