How to convert a string (eg ABCD12134) i.e a combination of text and numbers to a double ?
1 view (last 30 days)
Show older comments
I am trying to convert a string that has both letters and numbers to a double, I searched a lot but could'nt find a way. str2double dosent work and gives a NAN as the string contains letters as well. Please help me out if anyone has faced this problem before.
0 Comments
Answers (1)
Stephen23
on 15 Nov 2018
>> s = 'ABCD12134';
>> sscanf(s,'%*[^0123456789]%f')
ans = 12134
>> sscanf(s,'%*[A-Z]%f')
ans = 12134
>> str2double(regexp(s,'\d+','match'))
ans = 12134
2 Comments
Guillaume
on 27 Nov 2018
double is a storage format for numbers only. A double cannot store ABCD. So it's really not clear what you want. The numeric part of your char array can be stored in double, not the textual part.
Unless, your char array is the hexadecimal representation of a number and you want to convert that representation to its actual decimal value. But that's not what you asked at all so far.
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!