python读取.mat文件时出现’NotImplementedError: Please use HDF reader for matlab v7.3 files‘问题的解决方案
2017-09-20 22:03
741 查看
原文网址:http://www.th7.cn/Program/Python/201702/1098085.shtml
#最初用loadmat读取数据
#如果报错:NotImplementedError:Please use HDF reader for matlab v7.3 files
更改为h5py方式读取
mat文件里可能有多个cell,各对应着一个dataset
可以用keys方法查看cell的名字
可以用values方法查看各个cell的信息
可以用shape查看维度信息
注意,这里看到的shape信息与你在matlab打开的不同
这里的矩阵是matlab打开时矩阵的转置
所以,我们需要将它转置回来
再将其存为npy格式文件
npy文件的读取很简单
可以重新读取npy文件保存为mat文件
方法一(在MATLAB双击打开时遇到了错误:Unable to read MAT-file *.mat. Not a binary MAT-file. Try load -ASCII to read as text. ):
这里不会将数据转置
方法二(使用scipy):
#最初用loadmat读取数据
import numpy as np from scipy import io mat = io.loadmat('test.mat')
#如果报错:NotImplementedError:Please use HDF reader for matlab v7.3 files
更改为h5py方式读取
import h5py mat = h5py.File('test.mat')
mat文件里可能有多个cell,各对应着一个dataset
可以用keys方法查看cell的名字
print(mat.keys())
可以用values方法查看各个cell的信息
print(mat.values())
可以用shape查看维度信息
print(mat['your_dataset_name'].shape)
注意,这里看到的shape信息与你在matlab打开的不同
这里的矩阵是matlab打开时矩阵的转置
所以,我们需要将它转置回来
mat_t = np.transpose(mat['your_dataset_name']) mat_t 是numpy.ndarray格式
再将其存为npy格式文件
np.save('test.npy', mat_t)
npy文件的读取很简单
import numpy as np matrix = np.load('yourfile.npy')
可以重新读取npy文件保存为mat文件
方法一(在MATLAB双击打开时遇到了错误:Unable to read MAT-file *.mat. Not a binary MAT-file. Try load -ASCII to read as text. ):
import numpy as np matrix = np.load('yourfile.npy')
f = h5py.File('yourfile.mat', 'w')
f.create_dataset('dataname', data=matrix)
这里不会将数据转置
方法二(使用scipy):
from scipy import io mat = np.load('rlt_gene_features.npy-layer-3-train.npy') io.savemat('gene_features.mat', {'gene_features': mat})
相关文章推荐
- python读取.mat文件时出现’NotImplementedError: Please use HDF reader for matlab v7.3 files‘问题的解决方案
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- 编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- python32版本,出现问题“struct.error: argument for 's' must be a bytes object”的解决办法
- 【转】解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- DT升级中出现的问题的解决方案(Android upgrade ADT 22, R.java files not generated,java.lang.ClassNotFoundException)
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- [解决方案]Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
- 遇到问题——IntelliSense: #error directive: Please use the /MD switch for _AFXDLL builds
- 解决编译apache出现的问题:configure: error: APR not found . Please read the documentation