Borrar filtros
Borrar filtros

Reading binary files in matlab

11 visualizaciones (últimos 30 días)
Aubai
Aubai el 2 de Nov. de 2022
Comentada: Aubai el 10 de Nov. de 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
Args:
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
Note:
This function assumes a specific raw data format:
6 bytes integer followed by double (8 bytes float)
Returns:
data: an ndarray of tuples: (ndarray, float)
sample:
[([84, 1, 0, 0, 0, 0], 0.476667), ([62, 5, 0, 0, 0, 0], -0.571667), ... ]
"""
data = None
if os.path.exists(path):
try:
with open(path, 'rb') as imc_file:
# read in data file and search for start of raw data
data = imc_file.read()
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')
data.append([x,y])
datax.append(x)
datay.append(y)
#print(f'x={x}, y={y}')
plt.plot(datax,datay)
plt.show()
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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by