Problem: I am trying to ready in a binary data file. The format of the file is a series of data "blocks" that each contain various data types in a repeating pattern. Example: A file contains a series of data "blocks" that are 17 bytes each, and there are 20 entries (for a total of 340 bytes). The first 8 bytes (64 bits) of a given "block" represent a double, the next byte is an unsigned 8-bit integer, the next 4 bytes represent a single, and the final 4 bytes are a 32-bit signed integer. This pattern is then repeated for all 20 entries.
I currently am using two nested "for" loops to read in these data types one at a time with the "fread" function. This works, but is very slow, especially for large files. What I'm attempting to do now is read in all the data at once as a series of uint8 values (which is much faster), then reshape it to a matrix (in this example, 20x17) and convert the values to the data type that I desire (e.g., take the first 8 columns of each row (20x8) and convert them into a double (20x1)).
I don't know of an easy way to do this. I could write the data to a temporary binary file and then read it back in as a new data type and remove the file, but I would rather not bother with the file I/O, there should be a way to do it in the workspace. The only other option I can think of is to convert the uint8 values into a binary string and manually reconstruct the new data type from the bits, but if there was a simpler built-in (and faster, and less error-prone) way to accomplish this I would prefer it.
Any suggestions are appreciated, thanks.