MATLAB: Converting a uint32 (4-byte) value to the corresponding IEEE single-precision floating-point form

13,633

Solution 1

I think what you're saying is that the underlying bits represent a floating point number, but that you've got it stored as a uint32.

If that's that case, you can cast it (i.e. reinterpret the bits) as a single precision float using the typecast() function.

b = typecast(a, 'single')

where a is your variable.

See: http://www.mathworks.com/help/techdoc/ref/typecast.html

Edited: not the cast function, the typecast function... My apologies!

Solution 2

You could do the cast when you read the data in with fread().

Have a look for the precision argument, you could read it as the int32 number and store it as a single by doing

shut_speed=fread(fid,1,'int32=>single');
Share:
13,633
Ole Thomsen Buus
Author by

Ole Thomsen Buus

Updated on June 21, 2022

Comments

  • Ole Thomsen Buus
    Ole Thomsen Buus almost 2 years

    In MATLAB (r2009b) I have a uint32 variable containing the value 2147484101.

    This number (its 4-bytes) has been extracted from a digital machine-vision camera in a grabbing process. According to what I understand it holds the single-precision form of the shutter-speed of the camera (should be close to 1/260s = 3.8ms).

    How do I convert this 32-bit number to its IEEE single-precision floating-point representation - using what's available in MATLAB?

    With mentioned value in variable n, I have tried using a combination of nn=dec2hex(n,16) and then hex2num(nn). But it seems that hex2num expects the hexadecimal coding to be double-precision and not single as it is here. Atleast I am getting weird numbers with this method.

    Any ideas?

    Edit: Tried @Matt's answer below:

    typecast(uint32(2147484101),'single') %# without swapbytes
    typecast(swapbytes(uint32(2147484101)),'single') %# with swapbytes
    

    Which gives:

    ans =
    
      -6.3478820e-043
    
    ans =
    
      -2.0640313e+003
    

    I tried the IEEE 754 converter (JAVA applet) at http://www.h-schmidt.net/FloatApplet/IEEE754.html.

    Using:

    format hex
    typecast(uint32(2147484101),'uint8') %# without swapbytes
    typecast(swapbytes(uint32(2147484101)),'uint8') %# with swapbytes
    

    gives

    ans =
    
       c5   01   00   80
    
    ans =
    
       80   00   01   c5
    

    Entering these bytes into the applet (hexadecimal) gives me the same numbers as MATLAB.

  • Ole Thomsen Buus
    Ole Thomsen Buus about 13 years
    Yep, it must be typecast and not cast. But I am not really getting values I hoped for. You see, I have no prior-knowledge about the numbers. I am not even sure I got them stored correctly :) I just cross-checked using a java applet. So your method is sound. +1 and accepted.
  • Ole Thomsen Buus
    Ole Thomsen Buus about 13 years
    Yes that is also a possibility - had not thought of that one.
  • Matt
    Matt about 13 years
    @Ole Thomsen Buus: It's hard to suggest what you might do differently without knowing the method by which you arrived at your uint32, or the specification of the file/device you're reading. Hopefully you'll figure out where it's going wrong...