fread a double that's been broken up into four 16 bit words
Show older comments
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
Star Strider
on 10 Mar 2026
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.
James Tursa
on 10 Mar 2026
Edited: James Tursa
on 10 Mar 2026
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.
Accepted Answer
More Answers (2)
John D'Errico
on 10 Mar 2026
Edited: John D'Errico
on 10 Mar 2026
I don't see the problem. For example...
Xtest = randn*100
Now, I'll split it into 4 hex numbers, each taking up 16 bits.
Xhex = num2hex(Xtest)
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)
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.
Anthony
on 11 Mar 2026
0 votes
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!