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

关键词**: MATLABMATpython

#mermaid-svg-idMTV6KnkvH3Ei1F .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .label text{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .node rect,#mermaid-svg-idMTV6KnkvH3Ei1F .node circle,#mermaid-svg-idMTV6KnkvH3Ei1F .node ellipse,#mermaid-svg-idMTV6KnkvH3Ei1F .node polygon,#mermaid-svg-idMTV6KnkvH3Ei1F .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-idMTV6KnkvH3Ei1F .node .label{text-align:center;fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .node.clickable{cursor:pointer}#mermaid-svg-idMTV6KnkvH3Ei1F .arrowheadPath{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-idMTV6KnkvH3Ei1F .flowchart-link{stroke:#333;fill:none}#mermaid-svg-idMTV6KnkvH3Ei1F .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-idMTV6KnkvH3Ei1F .edgeLabel rect{opacity:0.9}#mermaid-svg-idMTV6KnkvH3Ei1F .edgeLabel span{color:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-idMTV6KnkvH3Ei1F .cluster text{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-idMTV6KnkvH3Ei1F .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-idMTV6KnkvH3Ei1F text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-idMTV6KnkvH3Ei1F .actor-line{stroke:grey}#mermaid-svg-idMTV6KnkvH3Ei1F .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-idMTV6KnkvH3Ei1F #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .sequenceNumber{fill:#fff}#mermaid-svg-idMTV6KnkvH3Ei1F #sequencenumber{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F #crosshead path{fill:#333;stroke:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .messageText{fill:#333;stroke:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-idMTV6KnkvH3Ei1F .labelText,#mermaid-svg-idMTV6KnkvH3Ei1F .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-idMTV6KnkvH3Ei1F .loopText,#mermaid-svg-idMTV6KnkvH3Ei1F .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-idMTV6KnkvH3Ei1F .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-idMTV6KnkvH3Ei1F .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-idMTV6KnkvH3Ei1F .noteText,#mermaid-svg-idMTV6KnkvH3Ei1F .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-idMTV6KnkvH3Ei1F .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-idMTV6KnkvH3Ei1F .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-idMTV6KnkvH3Ei1F .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-idMTV6KnkvH3Ei1F .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .section{stroke:none;opacity:0.2}#mermaid-svg-idMTV6KnkvH3Ei1F .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-idMTV6KnkvH3Ei1F .section2{fill:#fff400}#mermaid-svg-idMTV6KnkvH3Ei1F .section1,#mermaid-svg-idMTV6KnkvH3Ei1F .section3{fill:#fff;opacity:0.2}#mermaid-svg-idMTV6KnkvH3Ei1F .sectionTitle0{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .sectionTitle1{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .sectionTitle2{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .sectionTitle3{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-idMTV6KnkvH3Ei1F .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .grid path{stroke-width:0}#mermaid-svg-idMTV6KnkvH3Ei1F .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-idMTV6KnkvH3Ei1F .task{stroke-width:2}#mermaid-svg-idMTV6KnkvH3Ei1F .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .taskText:not([font-size]){font-size:11px}#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-idMTV6KnkvH3Ei1F .task.clickable{cursor:pointer}#mermaid-svg-idMTV6KnkvH3Ei1F .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-idMTV6KnkvH3Ei1F .taskText0,#mermaid-svg-idMTV6KnkvH3Ei1F .taskText1,#mermaid-svg-idMTV6KnkvH3Ei1F .taskText2,#mermaid-svg-idMTV6KnkvH3Ei1F .taskText3{fill:#fff}#mermaid-svg-idMTV6KnkvH3Ei1F .task0,#mermaid-svg-idMTV6KnkvH3Ei1F .task1,#mermaid-svg-idMTV6KnkvH3Ei1F .task2,#mermaid-svg-idMTV6KnkvH3Ei1F .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutside0,#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutside2{fill:#000}#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutside1,#mermaid-svg-idMTV6KnkvH3Ei1F .taskTextOutside3{fill:#000}#mermaid-svg-idMTV6KnkvH3Ei1F .active0,#mermaid-svg-idMTV6KnkvH3Ei1F .active1,#mermaid-svg-idMTV6KnkvH3Ei1F .active2,#mermaid-svg-idMTV6KnkvH3Ei1F .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-idMTV6KnkvH3Ei1F .activeText0,#mermaid-svg-idMTV6KnkvH3Ei1F .activeText1,#mermaid-svg-idMTV6KnkvH3Ei1F .activeText2,#mermaid-svg-idMTV6KnkvH3Ei1F .activeText3{fill:#000 !important}#mermaid-svg-idMTV6KnkvH3Ei1F .done0,#mermaid-svg-idMTV6KnkvH3Ei1F .done1,#mermaid-svg-idMTV6KnkvH3Ei1F .done2,#mermaid-svg-idMTV6KnkvH3Ei1F .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-idMTV6KnkvH3Ei1F .doneText0,#mermaid-svg-idMTV6KnkvH3Ei1F .doneText1,#mermaid-svg-idMTV6KnkvH3Ei1F .doneText2,#mermaid-svg-idMTV6KnkvH3Ei1F .doneText3{fill:#000 !important}#mermaid-svg-idMTV6KnkvH3Ei1F .crit0,#mermaid-svg-idMTV6KnkvH3Ei1F .crit1,#mermaid-svg-idMTV6KnkvH3Ei1F .crit2,#mermaid-svg-idMTV6KnkvH3Ei1F .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-idMTV6KnkvH3Ei1F .activeCrit0,#mermaid-svg-idMTV6KnkvH3Ei1F .activeCrit1,#mermaid-svg-idMTV6KnkvH3Ei1F .activeCrit2,#mermaid-svg-idMTV6KnkvH3Ei1F .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-idMTV6KnkvH3Ei1F .doneCrit0,#mermaid-svg-idMTV6KnkvH3Ei1F .doneCrit1,#mermaid-svg-idMTV6KnkvH3Ei1F .doneCrit2,#mermaid-svg-idMTV6KnkvH3Ei1F .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-idMTV6KnkvH3Ei1F .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-idMTV6KnkvH3Ei1F .milestoneText{font-style:italic}#mermaid-svg-idMTV6KnkvH3Ei1F .doneCritText0,#mermaid-svg-idMTV6KnkvH3Ei1F .doneCritText1,#mermaid-svg-idMTV6KnkvH3Ei1F .doneCritText2,#mermaid-svg-idMTV6KnkvH3Ei1F .doneCritText3{fill:#000 !important}#mermaid-svg-idMTV6KnkvH3Ei1F .activeCritText0,#mermaid-svg-idMTV6KnkvH3Ei1F .activeCritText1,#mermaid-svg-idMTV6KnkvH3Ei1F .activeCritText2,#mermaid-svg-idMTV6KnkvH3Ei1F .activeCritText3{fill:#000 !important}#mermaid-svg-idMTV6KnkvH3Ei1F .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-idMTV6KnkvH3Ei1F g.classGroup text .title{font-weight:bolder}#mermaid-svg-idMTV6KnkvH3Ei1F g.clickable{cursor:pointer}#mermaid-svg-idMTV6KnkvH3Ei1F g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-idMTV6KnkvH3Ei1F g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-idMTV6KnkvH3Ei1F .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-idMTV6KnkvH3Ei1F .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-idMTV6KnkvH3Ei1F .dashed-line{stroke-dasharray:3}#mermaid-svg-idMTV6KnkvH3Ei1F #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F .commit-id,#mermaid-svg-idMTV6KnkvH3Ei1F .commit-msg,#mermaid-svg-idMTV6KnkvH3Ei1F .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-idMTV6KnkvH3Ei1F g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-idMTV6KnkvH3Ei1F g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-idMTV6KnkvH3Ei1F g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-idMTV6KnkvH3Ei1F .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-idMTV6KnkvH3Ei1F .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-idMTV6KnkvH3Ei1F .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-idMTV6KnkvH3Ei1F .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-idMTV6KnkvH3Ei1F .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-idMTV6KnkvH3Ei1F .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-idMTV6KnkvH3Ei1F .edgeLabel text{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-idMTV6KnkvH3Ei1F .node circle.state-start{fill:black;stroke:black}#mermaid-svg-idMTV6KnkvH3Ei1F .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-idMTV6KnkvH3Ei1F #statediagram-barbEnd{fill:#9370db}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-state .divider{stroke:#9370db}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-idMTV6KnkvH3Ei1F .note-edge{stroke-dasharray:5}#mermaid-svg-idMTV6KnkvH3Ei1F .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-idMTV6KnkvH3Ei1F .error-icon{fill:#522}#mermaid-svg-idMTV6KnkvH3Ei1F .error-text{fill:#522;stroke:#522}#mermaid-svg-idMTV6KnkvH3Ei1F .edge-thickness-normal{stroke-width:2px}#mermaid-svg-idMTV6KnkvH3Ei1F .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-idMTV6KnkvH3Ei1F .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-idMTV6KnkvH3Ei1F .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-idMTV6KnkvH3Ei1F .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-idMTV6KnkvH3Ei1F .marker{fill:#333}#mermaid-svg-idMTV6KnkvH3Ei1F .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-idMTV6KnkvH3Ei1F {color: rgba(0, 0, 0, 0.75);font: ;}

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变量:

▲ 图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 h5pyfilename = 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_fidfid = h5f.open(name, flags, fapl=fapl)File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapperFile "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapperFile "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 mat4pyDownloading 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 loadmatfilename = 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中读取的上述数据中变量名称。

▲ 图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的尺寸描述:

▲ 图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对应的图片:

▲ 图1.2.3 imgdata对应的图片:

§02 总  结


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

  利用手边已有的用于 2021年人工神经网络第四次作业要求 中存储的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)

▲ 图2.1 数据库中的文件


■ 相关文献链接:

  • Read Matlab mat Files in Python
  • 2021年人工神经网络第四次作业要求

● 相关图表链接:

  • 图1.1.1 存储成ASCII格式的p变量:
  • 图1.2.1 MATLAB 数据内部的变量
  • 图1.2.2 MATLAB中关于images的尺寸描述。
  • 图1.2.3 imgdata对应的图片:
  • 图2.1 数据库中的文件

在Python中读取MATLAB的数据文件相关推荐

  1. [转载] pythonpandas读取csv文件最后一行_简单小案例(一):使用Pandas在Python中读取和写入CSV文件...

    参考链接: 使用Pandas在Python中读写CSV文件 前期文章链接: YOLOv3论文 https://arxiv.org/pdf/1804.02767.pdf 正文: 有许多方法可以在Pyth ...

  2. Python之数据分析(Numpy中读取与保存数据文件、将数据文件制成K线图)

    文章目录 一.读取与保存文件 二.将数据文件制成K线图 一.读取与保存文件 1.读取文件格式 numpy.loadtxt(文件名,delimiter = 分割符,usecols = 选择列,unpac ...

  3. csv java web 导入_DAY5-step9 使用CSV模块和Pandas在Python中读取和写入CSV文件

    什么是CSV文件? CSV文件是一种纯文本文件,其使用特定的结构来排列表格数据. CSV是一种紧凑,简单且通用的数据交换通用格式. 许多在线服务允许其用户将网站中的表格数据导出到CSV文件中. CSV ...

  4. pythonpandas读取csv文件最后一行_简单小案例(一):使用Pandas在Python中读取和写入CSV文件...

    前期文章链接: YOLOv3论文 https://arxiv.org/pdf/1804.02767.pdf 正文: 有许多方法可以在Python中读写CSV文件.有几种不同的方法,例如,您可以使用Py ...

  5. 用python的pandas打开csv文件_使用CSV模块和Pandas在Python中读取和写入CSV文件

    什么是CSV文件? CSV文件是一种纯文本文件,其使用特定的结构来排列表格数据.CSV是一种紧凑,简单且通用的数据交换通用格式.许多在线服务允许其用户将网站中的表格数据导出到CSV文件中.CSV文件将 ...

  6. Python pandas 读取csv/txt数据文件 python读取csv/txt文件

    导读 主要利用pandas.read_csv接口对csv格式文件或txt文件进行读取,由于CSV格式文件使用非常频繁,功能强大,参数众多,因此在这里专门做详细介绍 使用示例 # 基础用法 import ...

  7. python处理表格数据-用Python创建/读取/导出表格数据文件

    一.创建数据集 R语言中创建或导入数据框是非常常见的,只需要一个data.frame的命令就可以了,这个是R语言的基本命令. 我不习惯用Python进行表格式的数据操作和分析,这次和阿雷一起学习如何在 ...

  8. 在python中读取和写入CSV文件(你真的会吗?)

  9. python读取多行json_如何在Python中读取包含多个JSON对象的JSON文件?

    所以这是在python中读取JSON文件的标准方法 import json from pprint import pprint with open('ig001.json') as data_file ...

最新文章

  1. 025_Excel知识汇总
  2. matlab的rvctools,rvctools 机器人工具箱,主要用于 仿真, 型很强。 matlab 270万源代码下载- www.pudn.com...
  3. jsoup 获取指定页面的所有链接(需后续完善)
  4. C++零食:WTL中使用双缓冲避免闪烁
  5. 一个div 上下两行_用 Python 撸一个 Web 服务器第4章:动态渲染数据
  6. Java中File类的createNewFile、mkdir与mkdirs区别
  7. boost::mpl模块实现map相关的测试程序
  8. 幼小衔接语言教案上c册_关于幼小衔接,这里有你最想要的解答
  9. 没有bug队——加贝——Python 练习实例 23,24
  10. Android下写一个永远不会被KILL掉的进程/服务
  11. python输出字典_Python如何将字典键和值拆分为单独的列表?(代码示例)
  12. android-eclips中logcat不显示信息的问题解决
  13. vivado2018.3添加IP库
  14. 【leetcode_easy】538. Convert BST to Greater Tree
  15. openSUSE 安装 Torch
  16. 数据结构视频教程哪个好
  17. eclipse html插件的下载和安装
  18. 【自动驾驶传感器融合系列】01自动驾驶中常用的传感器
  19. 计算机通信答辩ppt,电子设计大赛答辩.ppt
  20. 服务器硬件维护指南和解决方案

热门文章

  1. 【JSConf EU 2018】WebAssembly 的手工艺术
  2. Redis学习笔记~Redis在windows环境下的安装
  3. layer.load 支持文字内容
  4. Python中os和shutil模块实用方法集锦
  5. memcached原理详述及配置
  6. Java-Reflection反射-获取包括父类在内的所有字段
  7. ASP.NET MVC的View是如何呈现出来的[实例篇]
  8. bad geometry: block count 65536 exceeds size of device (53248 blocks)
  9. 一篇为你讲透Yii2的widget这货
  10. Configure Drill