简 介: 本文测试了几种通过python直接读取MATLAB的**.MAT格式的数据文件,有些方法经过测试发现无法完成。而通过mat4py可以比较方便的读取MATLAB中的数据文件。利用手边已有存储的MATLAB的数据文件,可以测试所得到的文件数据是正确的。需要注意到是,使用mat4py读取MATLAB数据文件中,MATLAB数据文件中不能够有超过2维的数组!否则在读取的过程中mat4py会报错!


MATLAB数据

文章目录

MATLAB数据存储

基本方法

存储成ASCII格式

使用Python读取
MATLAB数据文件

io

NumPy

mat4py

总 结

 

§01 MATLAB数据


1.1 MATLAB数据存储

1.1.1 基本方法

  MATLAB中的数据存储最简单的方法是 save 命令,将MATLAB中的变量数据存储在文件系统中。

>> help save
save - 将工作区变量保存到文件中

    此 MATLAB 函数 将当前工作区中的所有变量保存在 MATLAB 格式的二进制文件(MAT 文件)filename 中。如果 filename
    已存在,save 会覆盖该文件。

    save(filename)
    save(filename,variables)
    save(filename,variables,fmt)
    save(filename,variables,version)
    save(filename,variables,version,'-nocompression')
    save(filename,variables,'-append')
    save(filename,variables,'-append','-nocompression')
    save filename

  读取数据文件使用 load命令,都区之后会自动在MATLAB环境中产生对应的数据变量。

>> help load
load - 将文件变量加载到工作区中

    此 MATLAB 函数 从 filename 加载数据。

    load(filename)
    load(filename,variables)
    load(filename,'-ascii')
    load(filename,'-mat')
    load(filename,'-mat',variables)
    S = load(___)
    load filename

1.1.2 存储成ASCII格式

  可以通过对 save 增加 存储格式参数,将数据存储成 ASCII 文本形式,这样可以便于其它程序读取数据。

>> save('d:\temp\q.txt', 'p', '-ascii')

  存储成ASCII格式的p变量:

Python读matlab python读matlabopaque_matlab


▲ 图1.1.1 存储成ASCII格式的p变量:


  存储成ASCII文件格式存储一下缺点:

  • 但并不是所有的格式都适合存储成ASCII格式。
  • 存储文件体量比较大。

  因此使用 .MAT格式仍然是非常重要的存储方式。

  那么,如何在Python中读取MATLABMAT格式的数据文件呢?

1.2 使用Python读取MATLAB数据文件

  在 Read Matlab mat Files in Python 介绍了利用Python来读取MATLAB数据文件几种方法。

  • Use the scipy.io Module to Read .mat Files in Python
  • Use the NumPy Module to Read mat Files in Python
  • Use the mat4py Module to Read mat Files in Python
  • Use the matlab.engine Module to Read mat Files in Python

1.2.1 io

  利用下面的代码段,测试scipy.io读取 .MAT文件。

import scipy
filename = r'D:\Temp\ANN-DATASET\FORBIDDEN\xraydata-96-80.mat'
mat = scipy.io.loadmat(filename)

  但是在Windows7测试发现会出现错误:

Traceback (most recent call last):
  File "D:\Temp\TEMP0001\test2.PY", line 14, in <module>
    mat = scipy.io.loadmat(filename)
AttributeError: module 'scipy' has no attribute 'io'

1.2.2 NumPy

  测试 h5py读取MATLAB文件。

import numpy as np
import h5py

filename = r'D:\Temp\ANN-DATASET\FORBIDDEN\xraydata-96-80.mat'
f = h5py.File(filename, 'r')

  出现了如下的错误:

Traceback (most recent call last):
  File "D:\Temp\TEMP0001\test2.PY", line 15, in <module>
    f = h5py.File(filename, 'r')
  File "C:\Users\zhuoqing\Anaconda3\lib\site-packages\h5py\_hl\files.py", line 408, in __init__
    swmr=swmr)
  File "C:\Users\zhuoqing\Anaconda3\lib\site-packages\h5py\_hl\files.py", line 173, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5f.pyx", line 88, in h5py.h5f.open
OSError: Unable to open file (file signature not found)

1.2.3 mat4py

(1)mat4py

python -m pip install mat4py

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mat4py
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e8/56/41b3ffd7b5f3eb3056979e0dc37de184c6e5dacd1a353e5de42323e7d138/mat4py-0.5.0-py2.py3-none-any.whl
Installing collected packages: mat4py
Successfully installed mat4py-0.5.0
(2)读取数据文件
from mat4py import loadmat

filename = r'D:\Temp\ANN-DATASET\FORBIDDEN\xraydata-96-80.mat'

data = loadmat(filename)

printf(type(data))
printf(data.keys())

  显示的结果:

<class 'dict'>
dict_keys(['images', 'labels', 'labeldir', 'imagesize'])

  上面结果可以看到读取的结果是一个字典。 对应的keys就是MATLAB中的变量名称。

  下面显示是在MATLAB中读取的上述数据中变量名称。

Python读matlab python读matlabopaque_python_02


▲ 图1.2.1 MATLAB 数据内部的变量


(3)显示变量格式
 Ⅰ.显示所有的keys
for k in data.keys():
    print(type(data[k]))
<class 'dict'>
dict_keys(['images', 'labels', 'labeldir', 'imagesize'])
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
 Ⅱ.显示labels
label = data['labels']
print(type(label))
print(len(label))
print(label)
<class 'list'>
6401
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
 Ⅲ.显示imagesize
data1 = data['imagesize']
print(data1)

[96, 80]

 Ⅳ.显示images
imgdata = data['images']
print(type(imgdata))
print(len(imgdata))
print(shape(imgdata))

<class 'list'>6401(6401, 7680)

  这与MATLAB中关于image的描述不同。
  MATLAB中关于images的尺寸描述:

Python读matlab python读matlabopaque_开发语言_03


▲ 图1.2.2 MATLAB中关于images的尺寸描述。


id = imgdata[0]
print(type(id))
print(shape(id))
print(len(id))

<class 'list'>(7680,)7680

print(id)
[207, 207, 208, 210, 211, 213, 215, 214, 215, 216, 218, 220, 221, 221, 222, 224, 226, 227, 227, 227, 227, 227, 229, 231, 232, 232, 232, 231, 230, 231, 233, 233, 232, 231, 230, 231, 232, 233, 234, 234, 234, 234, 234, 234, 234, 232, 232, 231, 230, 230, 231, 232, 233, 233, 232, 232, 231, 231, 232, 232, 232, 231, 230, 230, 229, 229, 228, 228, 228, 228, 227, 226, 225, 225, 224, 224, 224, 224, 224, 223, 222, 222, 222, 221, 220, 220, 218, 216, 215, 214, 212, 210, 209, 207, 204, 205, 208, 208, 208, 210, 212, 214, 215, 215, 215, 217, 219, 220, 221, 221, 222, 224, 226, 227, 227, 228, 227, 228, 229, 231, 232, 233, 232, 231, 231, 232, 233, 234, 233, 231, 231, 232, 233, 234, 235, 235, 235, 235, 235, 235, 234, 233, 233, 232, 230, 230, 231, 232, 233, 233, 232, 232, 232, 232, 233, 232, 232, 232, 231, 230, 230, 230, 229, 229, 229, 228, 228, 227, 225, 225, 224, 225, 225, 225, 224, 224, 223, 223, 223, 222, 221, 220, 218, 217, 215, 214, 213, 212, 209, 207, 205, 206, 208, 208, 209, 210, 212, 214, 216, 216, 216, 218, 219, 220, 221, 222, 223, 225, 227, 228, 228, 228, 228, 229, 230, 232, 233, 233, 233, 232, 231, 232, 233, 235, 234, 233, 232, 232, 234, 235, 236, 236, 235, 235, 236, 236, 235, 234, 233, 232, 231, 231, 232, 233, 233, 233, 233, 233, 232, 233, 233, 233, 233, 232, 232, 230, 230, 230, 230, 229, 229, 229, 228, 227, 226, 226, 225, 225, 226, 225, 225, 224, 224, 224, 223, 222, 221, 220, 219, 218, 216, 215, 214, 212, 210, 20
idimg = array(id).reshape([80,96])
plt.imshow(idimg)
pltshow()

  imgdata对应的图片:

Python读matlab python读matlabopaque_开发语言_04


▲ 图1.2.3 imgdata对应的图片:


 

§02 总


本文测试了几种通过python直接读取MATLAB的**.MAT**格式的数据文件,有些方法经过测试发现无法完成。而通过mat4py可以比较方便的读取MATLAB中的数据文件。

  利用手边已有存储的MATLAB的数据文件,可以测试所得到的文件数据是正确的。

  需要注意到是,使用mat4py读取MATLAB数据文件中,MATLAB数据文件中不能够有超过2维的数组!否则在读取的过程中mat4py会报错!

gifdir = '/home/aistudio/work/GIF'

filedim = os.listdir(gifdir)
print(filedim)

for f in filedim:
    fn = os.path.join(gifdir, f)
    if os.path.isfile(fn):
        os.remove(fn)

for i in range(100):
    id = imgdata[i]
    idimg = array(id).reshape([80, 96])
    plt.imshow(idimg)

    fn = '%s/%d.jpg'%(gifdir, i)
    plt.savefig(fn)
    print(i)

Python读matlab python读matlabopaque_MATLAB_05


▲ 图2.1 数据库中的文件