Having problem converting a string to an integer using sscanf

Hi all, I'm having an issue converting a string to a number. See a copy of my problem below:
>> b = sscanf('123456789012345678', '%lu')
b =
4.2950e+009
Why is this happening? The same problem occurs all the way down to 10 digits. At 9, it returns the integer as expected. Your help is appreciated.

7 commentaires

hiaxis - I get the correct result on my 64-bit mac. What can you tell us about your computer and/or version of MATLAB? If you use computer as
[str,maxsize,endian] = computer
what is the value for each output parameter?
hiaxis's answer moved here
Thank you for the fast reply. I am running an old R14 SP3 version of MATLAB under Windows 10. It seems like this is a new problem for me but perhaps I'm remembering wrong. Here are the results of the command you provided:
>> [str,maxsize,endian] = computer
str =
PCWIN
maxsize =
2.1475e+009
endian =
L
OUTPUT OF COMPUTER VERSION OF MATLAB (32 bit / 64 bit)
PCWIN 32 bit MATLAB on Windows
So your output to sscanf may be reasonable given your version of the software...
Guillaume
Guillaume le 28 Jan 2020
Modifié(e) : Guillaume le 28 Jan 2020
In my version of matlab, I get a uint64 value out of that sscanf code whereas in the OP case it's clearly double. 4.295e9 is also suspiciously what you get from intmax('uint32') so I suspect that in R14, sscanf is limited to 32-bit integers. Even if it wasn't limited to 32 bit, the conversion to double would round the number to the nearest multiple of 16.
Maybe it's time to upgrade. R14SP3 came out 15 years ago!
You are probably correct. My PC is 64 bit but this is an old version of Matlab. However, it's interesting that the output as shown below tells me I can go longer on uint64 which is what I was trying to do. Am I misunderstanding?
>> intmax('uint64')
ans =
18446744073709551615
Yes, you could actually store the number if sscanf didn't convert to double. However since 123456789012345678 is greater than flintmax you'd be losing precision anyway (the number will get rounded down to 123456789012345680)
In any case, it does look like sscanf is limited to 32-bit numbers in your case.
Can you try textscan ? Do you get the same issue with textscan ?

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Question posée :

le 28 Jan 2020

Commenté :

le 29 Jan 2020

Community Treasure Hunt

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

Start Hunting!

Translated by