【问题标题】:load .mat file from python从 python 加载 .mat 文件
【发布时间】:2017-02-22 05:39:49
【问题描述】:

我正在尝试从 Python 运行 Matlab 中运行 Simulink 模式的脚本,将变量保存为 Power.mat 并在 Python 中读取此变量。我在 Windows 上使用 Python 2.7。

我尝试使用库 hdf5storage 来读取文件:

import hdf5storage
x=hdf5storage.loadmat('Power.mat','r')

但我收到了附加错误。

可能是什么问题? 我也尝试过使用库 h5py,但我得到了同样的错误。 文件 .mat 似乎没有损坏,因为我在 Matlab 中打开它没有任何问题。

谢谢!

【问题讨论】:

  • x=hdf5storage.loadmat('Power.mat') 应该可以工作,尽管下载测试它所需的包需要很长时间:)
  • 您是否将 Power.mat 保存为 7.3 版 MAT 文件?以前的版本不是 HDF5。来自 MATLAB:type('Power.mat') 将告诉您 MAT 文件版本。使用save('Power.mat', '-v7.3') 指定版本。
  • Read .mat files in Python的可能重复

标签: python matlab h5py hdf5storage


【解决方案1】:

试试这个代码:

import h5py
Data = h5py.File('File.mat')

【讨论】:

  • 这没用,它只是给了我一堆 hd5 参考对象。我对他们无能为力
【解决方案2】:

您可以使用 scipy.io 在 Python 和 Matlab 之间交换数据。为此目的,有名为 savemat 和 loadmat 的函数。

这样的事情应该可以工作:

import scipy.io
mat = scipy.io.loadmat('Power.mat')

供参考,http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html

【讨论】:

  • 有四种不同的.mat 文件格式。前三个v4v6v7 是专有的二进制格式。这些是scipy 可以打开的。最后一个,v7.3,是一个专门的 hdf5 文件。那是hdf5storage 可以打开的。从技术上讲,它可以被任何可以处理 hdf5 文件的东西打开,但是 hdf5storage 有一些工具可以优雅地处理 MATLAB 特定的位。否则你会得到一个非常丑陋(但最终仍然可用)的输出。
猜你喜欢
  • 1970-01-01
  • 2016-06-24
  • 2017-01-02
  • 2023-03-15
  • 2020-07-17
  • 2015-04-22
  • 2017-12-27
  • 2018-01-13
  • 2021-07-25
相关资源
最近更新 更多