How to convert a string (eg ABCD12134) i.e a combination of text and numbers to a double ?

1 view (last 30 days)
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.

Answers (1)

Stephen23
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
PARTH MISHRA
PARTH MISHRA on 27 Nov 2018
Thank you very much Stephen for the reply. So actually I want letters as well in my final answer not just the numbers. Can you suggest any way by which I can get numbers and text as my final answer i.e. ans = ABCD12134. But your answer is really helpful. Thanks alot
Guillaume
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!