0
>> str = '0009.51998'
>> str2num(str)

or

>> sscanf(str,'%f')

ans = 9.5200

I want to get this instead:

ans = 9.51998
1
  • if you already have a string, why are you suing num2str? (I realize this doesn't change your question/answer it) Commented Jan 12, 2012 at 17:56

1 Answer 1

3

You are getting that. It's just being rounded off to four decimal places when it's displayed. Do format long to see more precision.

>> str = '0009.51998';
>> x = sscanf(str, '%f')
x =
    9.5200
>> format long
>> x
x =
   9.519980000000000
>> 

You can also use str2double as an alternative to sscanf. It's safer and more flexible than str2num. That is because str2num uses the eval command. For example, try the following:

  str2num(' figure();imshow(''peppers.png'')')

You might be surprised at the results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.