fread a double that's been broken up into four 16 bit words

I have to read in a double from a file that's been broken up into 4 consecutive 16 bit words. How can I do that? I tried reading them in as four uint16 and then using bitconcat to put them back together, but that's not working.

3 Comments

What else do you know about the file?
The fread function optionally takes several arguments that might make this easier. Without the file and this additional information, I doubt that we could be of much help.
Unfortunately all I have is (not intuitve) idd. They haven 't given me a sample file to work with. That would make life a little easier. If I had that, I could try things and see if they worked. I'm using the 'rb' function on the fread. I'm sure that's got to be in there so that it reads it binarily, but I'm really at a loss.
You're going to need to give us a file sample. Otherwise, we are all guessing as to what the actual file format is. If it really is four consecutive 16-bit words, then as mentioned by others on this thread, fread (perhaps with swapbytes) should work fine.

Sign in to comment.

 Accepted Answer

Let's make four random 16 bit integers.
A = randi([intmin('uint16'), intmax('uint16')], 1, 4, 'uint16')
A = 1×4
48806 18820 16950 37969
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now we can typecast them into one double.
D1 = typecast(A, 'double')
D1 = -8.2026e-211
You might need to swapbytes on the integers before typecasting them.
B = swapbytes(A)
B = 1×4
42686 33865 13890 20884
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D2 = typecast(B, 'double')
D2 = 9.8163e+84
Let's look at the bit patterns:
format hex
A
A = 1×4
bea6 4984 4236 9451
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D1
D1 =
945142364984bea6
You can see that the four hex digits of the last element of A are the same as the first four hex digits of D1, the four hex digits of the next-to-last element make up the second four hex digits of D1, etc.
B
B = 1×4
a6be 8449 3642 5194
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D2
D2 =
519436428449a6be
The same holds for the hex digits of B and D2.

3 Comments

This seems to work fine if you're dealing with uint. I'm dealling with a signed Floating Point double where the decimal point can be anywhere, and it's being read in as four 16-bit words. I have to read it in 16 bits at a time and then recombine them and then convert it back into it's original number. For example, the number might be something like -120.2763850934, but it's been broken up bitwise into four 16 bit words. I would think I would need to read them in bitwise, combine them, and then put that into a double variable. I just can't seem to figure out how to read it in, much less combine it and then convert it into it's representative double. In case it matters, it's being read in as ieee-le.
Can you show us a small sample of how the number appears in the file, how you're reading the data into MATLAB now (what command you're using), and what the "signed Floating Point double where the decimal point can be anywhere" is being represented in MATLAB (as an fi object or some other type of object, perhaps? As a char array or string array containing digits like '12345.6789'?)
I understand why you wanted to see the input file so that you could see what I was talking about. The problem was between the keyboard and the chair. It was how I had the data formatted in my self-written "test" file. Feeling a little stupid right now. What I was doing was working, reading it in as four *uint16 and then using typecast to put it all together. Pretty simple, I just needed a good test file.

Sign in to comment.

More Answers (2)

I don't see the problem. For example...
Xtest = randn*100
Xtest = 67.8166
Now, I'll split it into 4 hex numbers, each taking up 16 bits.
Xhex = num2hex(Xtest)
Xhex = '4050f4426ed71e79'
Xhex is 64 bits. I could have written it out, then read the fragments back in, just pretend I did. But as long as you can concatenate the fragments into one vector, then hex2num does all the work for you.
hex2num(Xhex)
ans = 67.8166
So, if you can read it in as a string of 4 integers in hex form, then you can trivially reconstruct the number from the pieces.
Thanks for all the help. The problem was between the keyboard and the chair. It was how I was formatting my "test" file. I see why you were saying you need to see the input file to help me. Wish I had one.

Products

Release

R2024a

Asked:

on 10 Mar 2026

Edited:

on 11 Mar 2026

Community Treasure Hunt

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

Start Hunting!