Reading binary files in matlab
    7 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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
0 件のコメント
回答 (1 件)
  Shreyansh Mehra
    
 2022 年 11 月 8 日
        Hello,  
It is possible to directly run python code from MATLAB. The documentation linked below explores the same and might be of help.
参考
カテゴリ
				Help Center および File Exchange で Call Python from MATLAB についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

