Python中Matlab的'fread'相当于什么?

Dun*_*ait 12 python matlab numpy readline fread

我几乎不了解Matlab,需要将一些解析例程转换为Python.它们适用于大型文件,它们本身被划分为"块",并且我从文件顶部的校验和开始就很困难.

Matlab到底发生了什么?

status = fseek(fid, 0, 'cof');
fposition = ftell(fid);
disp(' ');
disp(['** Block ',num2str(iBlock),' File Position = ',int2str(fposition)]);

% ----------------- Block Start ------------------ %
[A, count] = fread(fid, 3, 'uint32');
if(count == 3)
    magic_l = A(1);
    magic_h = A(2);
    block_length = A(3);
else
    if(fposition == file_length)
        disp(['** End of file OK']);
    else
        disp(['** Cannot read block start magic !  Note File Length = ',num2str(file_length)]);
    end
    ok = 0;
    break;
end
Run Code Online (Sandbox Code Playgroud)

fid是当前正在查看的文件iBlock是一个计数器,你在文件中"阻止"它

magic_l和magic_h稍后与校验和有关,这里是代码(直接来自上面的代码):

disp(sprintf('  Magic_L = %08X, Magic_H = %08X, Length = %i', magic_l, magic_h, block_length));
correct_magic_l = hex2dec('4D445254');
correct_magic_h = hex2dec('43494741');

if(magic_l ~= correct_magic_l | magic_h ~= correct_magic_h)
    disp(['** Bad block start magic !']);
    ok = 0;
    return;
end

remaining_length = block_length - 3*4 - 3*4;   % We read Block Header, and we expect a footer
disp(sprintf('  Remaining Block bytes = %i', remaining_length));
Run Code Online (Sandbox Code Playgroud)
  • 这个%08X和那些hex2dec东西发生了什么?
  • 另外,为什么要指定3*4而不是12

实际上,我想知道如何[A, count] = fread(fid, 3, 'uint32');在Python中复制,io.readline()就像拉动文件的前3个字符一样.如果我在这里错过了某点,我会道歉.只是io.readline(3)在文件上使用似乎返回了它不应该返回的东西,并且我不明白block_length当它可能非常长时它如何适合单个字节.

感谢您阅读此漫步.我希望你能理解我想知道的一切!(任何见解都表示赞赏.)

Mat*_*kin 14

用于读取一维数组的Python代码

当用Python替换Matlab时,我想将二进制数据读入a numpy.array,所以我习惯numpy.fromfile将数据读入一维数组:

import numpy as np

with open(inputfilename, 'rb') as fid:
    data_array = np.fromfile(fid, np.int16)
Run Code Online (Sandbox Code Playgroud)

使用numpy.fromfile与其他Python解决方案相比的一些优点包括:

  • 无需手动确定要读取的项目数.您可以使用count=参数指定它们,但默认为-1指示读取整个文件.
  • 能够指定一个打开的文件对象(如上所述fid)或者您可以指定文件名.我更喜欢使用打开的文件对象,但是如果你想使用文件名,你可以用上面的两行替换:

    data_array = numpy.fromfile(inputfilename, numpy.int16)
    
    Run Code Online (Sandbox Code Playgroud)

二维数组的Matlab代码

Matlab fread有能力将数据读入表格矩阵,[m, n]而不是仅将其读入列向量.例如,要将数据读入2行矩阵,请使用:

fid = fopen(inputfilename, 'r');
data_array = fread(fid, [2, inf], 'int16');
fclose(fid);
Run Code Online (Sandbox Code Playgroud)

二维数组的等效Python代码

您可以使用Numpy shape和Python在Python中处理这种情况transpose.

import numpy as np

with open(inputfilename, 'rb') as fid:
    data_array = np.fromfile(fid, np.int16).reshape((-1, 2)).T
Run Code Online (Sandbox Code Playgroud)
  • -1讲述numpy.reshape来推断所述阵列基于Matlab的另一维度的等效该维度的长度inf无穷大表示.
  • .T,使得它与所述第一维度的具有轴a的2长度的2维数组的转置阵列.


Tor*_*rek 8

文档中fread可以看出,它是一个读取二进制数据的函数.第二个参数指定输出向量的大小,第三个参数指定读取的项的大小/类型.

为了在Python中重新创建它,您可以使用该array模块:

f = open(...)
import array
a = array.array("L")  # L is the typecode for uint32
a.fromfile(f, 3)
Run Code Online (Sandbox Code Playgroud)

这将读取文件中的三个uint32值f,这些值在a之后可用.来自以下文件fromfile:

从文件对象f中读取n个项目(作为机器值)并将它们附加到数组的末尾.如果少于n个项目可用,则会引发EOFError,但可用的项目仍会插入到阵列中.f必须是一个真正的内置文件对象; 使用read()方法的其他东西是不行的.

数组实现序列协议,因此支持与列表相同的操作,但您也可以使用该.tolist()方法从数组创建常规列表.