Arturs Vancans
Arturs Vancans

Reputation: 4640

Receive a floating point in Matlab from bytes

I would like to send a floating number over a serial COM to Matlab and interpret it as a single precision floating number.

I can send the data over the serial as byte packets.

So on sender side I shift 32bit floating point number by 8, mask it and send it

(byte)((number >> 8) & 0x000000FF)
(byte)((number >> 16) & 0x000000FF)
...
\r\n (line termination)

Now, on Matlab I connect to the COM and receive by using fscanf(s,'%f') where s is the serial instance. It reads the bytes till the termination line.

Now, the problem is that the output on Matlab is not the same as I sent. I believe the problem is whether in the different float representation or different sending order.

eg. If I send decimal 1.2 (0x3f99999a), Matlab prints 1.5315e+010

What could I be doing wrong?

EDIT:

Matlab fscanf http://www.mathworks.co.uk/help/matlab/ref/fscanf.html

Upvotes: 0

Views: 782

Answers (2)

Shan
Shan

Reputation: 5242

By default the number is of type double which is 64-bit. You should perform the shift for 64-bit instead of 32-bit. Otherwise you can declare the number as uint32 type and do the 32-bit shift.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283694

Try fread instead of fscanf, when the data is binary.

Upvotes: 1

Related Questions