【Python学习笔记】加载MATLAB文件:loadmat()解读

源码解析

def loadmat(file_name, mdict=None, appendmat=True, **kwargs):
    variable_names = kwargs.pop('variable_names', None)
    MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)
    matfile_dict = MR.get_variables(variable_names)
    if mdict is not None:
        mdict.update(matfile_dict)
    else:
        mdict = matfile_dict
    if file_opened:
        MR.mat_stream.close()
    return mdict

1. 参数

file_name: str

.mat文件的文件名(当appendmat = True时,不用加.mat后缀),也可以打开类似文件的对象(file-like object)

mdict: dict, 可选

插入文件变量的字典

appendmat: bool , 可选

若为真则在文件名后添加后缀

byte_order: str / None, 可选

默认为none。表示从mat文件中猜测的字节顺序,可以是“native”,“=”,“little”,“<”,“>”,“BIG”之一。

mat_dtype: bool,可选

若为真,则返回与加载到MATLAB中相同的dtype的数组,而不是保存数组的dtype。

squeeze_me: bool,可选

判断是否压缩单位矩阵的维数

chars_as_string: bool,可选.

将char数组转string数组

matlab_compatible: bool,可选

返回被MATLAB读取的数组(当squeeze_me = false, chars_as_string = false, mat_dtype = true, struct_as_record = true)

struct_as_record: bool,可选

设置flag来判断加载MATLAB以numpy记录数组还是以原形式numpy数组(dtype为对象)。

verify_compressed_data_integrity: bool,可选

MATLAB文件的长度是否已确认。

variable_names: None / sequence

若为None(默认),则读取文件中的所有变量。否则variable_names将是一个string序列,表示需要从mat文件中读取的变量名。此时读取器将跳过不是这个变量名的序列,一定程度上减少读取时间。

2. 返回

mat_dict: dict

以变量名为key,数组为values的字典

3. Notes

MATLAB 7.3及以上版本需要使用HDF5的库,scipy不支持该类版本mat文件的读取。

4. 样例

    >>> from os.path import dirname, join as pjoin
    >>> import scipy.io as sio

    Get the filename for an example .mat file from the tests/data directory.

    >>> data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data')
    >>> mat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat')

    Load the .mat file contents.

    >>> mat_contents = sio.loadmat(mat_fname)

    The result is a dictionary, one key/value pair for each variable:

    >>> sorted(mat_contents.keys())
    ['__globals__', '__header__', '__version__', 'testdouble']
    >>> mat_contents['testdouble']
    array([[0.        , 0.78539816, 1.57079633, 2.35619449, 3.14159265,
            3.92699082, 4.71238898, 5.49778714, 6.28318531]])

    By default SciPy reads MATLAB structs as structured NumPy arrays where the
    dtype fields are of type `object` and the names correspond to the MATLAB
    struct field names. This can be disabled by setting the optional argument
    `struct_as_record=False`.

    Get the filename for an example .mat file that contains a MATLAB struct
    called `teststruct` and load the contents.

    >>> matstruct_fname = pjoin(data_dir, 'teststruct_7.4_GLNX86.mat')
    >>> matstruct_contents = sio.loadmat(matstruct_fname)
    >>> teststruct = matstruct_contents['teststruct']
    >>> teststruct.dtype
    dtype([('stringfield', 'O'), ('doublefield', 'O'), ('complexfield', 'O')])

    The size of the structured array is the size of the MATLAB struct, not the
    number of elements in any particular field. The shape defaults to 2-D
    unless the optional argument `squeeze_me=True`, in which case all length 1
    dimensions are removed.

    >>> teststruct.size
    1
    >>> teststruct.shape
    (1, 1)

    Get the 'stringfield' of the first element in the MATLAB struct.

    >>> teststruct[0, 0]['stringfield']
    array(['Rats live on no evil star.'],
      dtype='>> teststruct['doublefield'][0, 0]
    array([[ 1.41421356,  2.71828183,  3.14159265]])

    Load the MATLAB struct, squeezing out length 1 dimensions, and get the item
    from the 'complexfield'.

    >>> matstruct_squeezed = sio.loadmat(matstruct_fname, squeeze_me=True)
    >>> matstruct_squeezed['teststruct'].shape
    ()
    >>> matstruct_squeezed['teststruct']['complexfield'].shape
    ()
    >>> matstruct_squeezed['teststruct']['complexfield'].item()
    array([ 1.41421356+1.41421356j,  2.71828183+2.71828183j,
        3.14159265+3.14159265j])

相关文档参见:https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html

你可能感兴趣的:(学习记录,Python之路,ML)