简体   繁体   English

我正在使用 MATLAB 和 python 读取 bin 文件。 他们以不同的方式阅读 header

[英]I am reading a bin file using MATLAB and python. They are reading header differently

I am trying to read a binary file using MATLAB and python.我正在尝试使用 MATLAB 和 python 读取二进制文件。 MATLAB is producing header MATLAB 正在生产 header

[80 0 8 0 1780 400 357 22441] whereas python is reading the header [80 0 8 0 1780 400 357 22441] 而 python 正在读取 header

[ 80 8 1780 357 258 22440 2040 2048]. [80 8 1780 357 258 22440 2040 2048]。

What is the wrong I am doing???我在做什么错??? I am running both on windows.我都在 windows 上运行。

**MATLAB code:** 
    fid = fopen(file_name);
    fread(fid,isTherePos(1)-1,'uint32','l'); 
    header = fread(fid,8,'uint16','l')

**Python code:** 
     with open(filename, mode='rb') as f:
       b = f.read()
       np_data = np.frombuffer(b, dtype='<I')
       srtOfLine = LidarMRC.findStartOfLine(filename)
       header = np_data[srtOfLine[0][0]:srtOfLine[0][0] +8].astype('<H')
       print(header)

In the MATLAB code, you read the first "isTherePos(1)-1" bytes as 32-bit values.在 MATLAB 代码中,您将第一个“isTherePos(1)-1”字节读取为 32 位值。 Then you read the header as 8x 16-bit values.然后您将 header 读取为 8x 16 位值。

In the Python code, you read the whole file into np_data as 32-bit values.在 Python 代码中,您将整个文件作为 32 位值读入 np_data。 The "astype" function does not re-interpret those bytes. “astype” function 不会重新解释这些字节。 It takes each 32-bit entity and truncates it to 16 bits, which is exactly what you see.它采用每个 32 位实体并将其截断为 16 位,这正是您所看到的。

You should read np_data as dtype=np.uint16 , then adjust your srtOfLine number to multiply by 2.您应该将 np_data 读取为 dtype dtype=np.uint16 ,然后将 srtOfLine 编号调整为乘以 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matlab和Python读取二进制文件的方式不同 - Matlab and Python Reading Binary File Differently 使用Python读取,添加和保存CSV文件。 - Reading, Adding to and saving a CSV File using Python. 在Python中读取.bin或.dat文件 - Reading .bin or .dat file in Python 我有一堆csv文件,我正在使用python中的pandas读取它们。 我想结合使用map和lambda函数来执行此操作 - I have a bunch of csv files, I am reading them using pandas from python. I want to use a combination of map & lambda functions to do this 用 Python 读取二进制文件 header - Reading a binary file header with Python 读取 python 中的 csv 文件。 我有编码错误的问题吗? - Reading csv file in python. Do I have an issue of wrong encoding? 将文件读入python列表中。 如何拿出单词 - Reading a file into a list on python. How to take out words 使用 Python 读取包含空单元格的 Excel 电子表格的内容。 我需要以编程方式忽略空单元格 - Reading contents of an Excel spreadsheet that includes empty cells using Python. I need to programmatically ignore empty cells 我正在通过解析器在python中读取文件,但无法正常工作 - I am reading file via parser in python but its not working python scrapy代码打印出我正在读取的文件 - python scrapy code prints out the file I am reading from
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM