Brett wrote:
>
> I guess I need to rephrase the question. With the fread command, if I
> tell it to read in an int8, it reads 8 bits and stores it in matlab as
> a double. But to create what fread would read when given the double
> option would require 64 bits. Is there an exisiting function to
> combine 4 8-bit words and interpret them as a double?
There's hex2num. Convert the 4 8-bit words to hex (a character
string using characters '0' through '9', 'A' through 'F'),
then use hex2num to convert to double.
Inefficient, but it should work.
Let's see:
> fprintf(1,'%bx',-2.423e-17)
bc7bef6f09bf8a00 % a valid double, in hex.
>> a=uint8(hex2dec(['bc7b';'ef6f';'09bf';'8a00']))
a =
255
255
255
255
Notice what happened here. Something is not adding up right.
If you really have 8-bit values, then 4 of them will only
add up to 32 bits, a single.
Here's the rest of the example, with uint16's:
>> a=uint16(hex2dec(['bc7b';'ef6f';'09bf';'8a00']))
a =
48251
61295
2495
35328
So this vector might represent what I read in as four
uint16s. Now let's convert it:
>> fprintf(1,'%x',double(a))
bc7bef6f9bf8a00
Just to validate the character string.
>> hex2num(sprintf('%x',double(a)))
ans =
-2.423000755955150e-17
Note that I used sprintf() to generate string output.
So something like that is your answer, except that I'm not sure
if you want to convert 4 uint8's to a single, or 8 uint8's to
a double, or 4 uint16's to a double, or...
- Randy
>
> I guess I need to rephrase the question. With the fread command, if I
> tell it to read in an int8, it reads 8 bits and stores it in matlab as
> a double. But to create what fread would read when given the double
> option would require 64 bits. Is there an exisiting function to
> combine 4 8-bit words and interpret them as a double?
There's hex2num. Convert the 4 8-bit words to hex (a character
string using characters '0' through '9', 'A' through 'F'),
then use hex2num to convert to double.
Inefficient, but it should work.
Let's see:
> fprintf(1,'%bx',-2.423e-17)
bc7bef6f09bf8a00 % a valid double, in hex.
>> a=uint8(hex2dec(['bc7b';'ef6f';'09bf';'8a00']))
a =
255
255
255
255
Notice what happened here. Something is not adding up right.
If you really have 8-bit values, then 4 of them will only
add up to 32 bits, a single.
Here's the rest of the example, with uint16's:
>> a=uint16(hex2dec(['bc7b';'ef6f';'09bf';'8a00']))
a =
48251
61295
2495
35328
So this vector might represent what I read in as four
uint16s. Now let's convert it:
>> fprintf(1,'%x',double(a))
bc7bef6f9bf8a00
Just to validate the character string.
>> hex2num(sprintf('%x',double(a)))
ans =
-2.423000755955150e-17
Note that I used sprintf() to generate string output.
So something like that is your answer, except that I'm not sure
if you want to convert 4 uint8's to a single, or 8 uint8's to
a double, or 4 uint16's to a double, or...
- Randy