Python has a way to read blocks of binary data and then unpack them from their original encoding into a more accessible form. For example, in some Python code I am using, I use the following code to accomplish this:
with open(filename, "rb") as binary_file:
# Read many records at once
data = binary_file.read(number_of_records*record_size)
fmt = "8B Q Q 2L 2L 2L 4H 4H 3H2B"
self.data_array = np.asarray(list(struct.iter_unpack("< " + fmt, data)))
I'd like to know how to do this in Matlab in the same efficient way, without having to read each variable individually.
Is there something in Matlab to do this?
One way to do this is to read the data using fread
and then convert using typecast
. I'm not familiar with the format you used, but say you have a collection of integers and floats. You can read them as one large collection of bytes and then covert.
fid = fopen('file.bin', 'rb');
data = fread(fid, 32, '*uint8');
ints = typecast(data(1:8), 'int32');
floats = typecast(data(9:end), 'single');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.