Reading binary files in matlab

10 views (last 30 days)
Aubai on 2 Nov 2022
Commented: Aubai on 10 Nov 2022
Dear All,
I have the following code in Python (which is working for me) how can i made the same code in Matlab?
import IMCtermite
import numpy as np
import os
import matplotlib.pyplot as plt
imc_path = r'C:\Users\00037218\Desktop\Disctop_Temp\Bet\FAMOS_Load_Data_New\RawData\MQTTClient_1_WGEN1_RotSpdGyro.raw'
def get_data(path, record_size, data_type):
Returns the data as numpy array
path: str The path of the IMC file to analyse
record_size: int The number of byte of a single raw data record entry
data_type: np.dtype Numpy data type, suitable raw data record structure
This function assumes a specific raw data format:
6 bytes integer followed by double (8 bytes float)
data: an ndarray of tuples: (ndarray, float)
[([84, 1, 0, 0, 0, 0], 0.476667), ([62, 5, 0, 0, 0, 0], -0.571667), ... ]
data = None
if os.path.exists(path):
with open(path, 'rb') as imc_file:
# read in data file and search for start of raw data
data =
n = data.find(b'|CS') # search for start of raw data block
for i in range(4): # raw data starts immediately after 4th ','
n = data.find(b',', n+1)
data = data[n+1:-1] # data = "raw data"; n+1 = first byte after ',' # -1 because of ';' as delimiter at the end of the raw data
length = len(data)
remainder = length % record_size
if remainder != 0:
print(f'WARNING: Data length is not as expect a multiple of {bytes}: {length} -> remainder={remainder}')
data = data[0:-remainder] # make data a multiple of the expected record size
data = np.frombuffer(data, dtype=data_type) # data is now numpy data array created from "raw data"
except Exception as e:
print(f'Exception occured: {e}')
return data
if __name__ == '__main__':
bytes1 = 6 + 8 # 6 bytes int values followed by 8 bytes float value
dtype = np.dtype([('x', 'u1', 6), ('y', 'f8')]) # HERE adapt data format according to raw data structure
RotSpdGyro = get_data(imc_path, bytes1, dtype)
data = []
datax = []
datay = []
for entry in RotSpdGyro:
a, y = entry
x = int.from_bytes(a, signed=False, byteorder='little')
#print(f'x={x}, y={y}')
main issue for me this the following:
how to program is line of code in matlab:
bytes1 = 6 + 8 # 6 bytes int values followed by 8 bytes float value
dtype = np.dtype([('x', 'u1', 6), ('y', 'f8')]) # HERE adapt data format according to raw data structure
and this one:
x = int.from_bytes(a, signed=False, byteorder='little')
so i can read this data
It is not an option to call Python from matlab (there is no python on the computer where this code should be running)
thanks in advance for you help
Best Regards

Answers (1)

Shreyansh Mehra
Shreyansh Mehra on 8 Nov 2022
It is possible to directly run python code from MATLAB. The documentation linked below explores the same and might be of help.
  1 Comment
Aubai on 10 Nov 2022
Unfortunatily not an option for me here

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by