python读取mat_如何在python中读取Mat v7.3文件?

1586010002-jmsa.png

I am trying to read the mat file given in the following website, ufldl.stanford.edu/housenumbers, in the file train.tar.gz, there is a mat file named digitStruct.mat.

when i used scipy.io to read the mat file, it alerts me with the message ' please use hdf reader for matlab v7.3 files '.

the original matlab file is provided as below

load digitStruct.mat

for i = 1:length(digitStruct)

im = imread([digitStruct(i).name]);

for j = 1:length(digitStruct(i).bbox)

[height, width] = size(im);

aa = max(digitStruct(i).bbox(j).top+1,1);

bb = min(digitStruct(i).bbox(j).top+digitStruct(i).bbox(j).height, height);

cc = max(digitStruct(i).bbox(j).left+1,1);

dd = min(digitStruct(i).bbox(j).left+digitStruct(i).bbox(j).width, width);

imshow(im(aa:bb, cc:dd, :));

fprintf('%d\n',digitStruct(i).bbox(j).label );

pause;

end

end

as shown above, the mat file has the key 'digitStruct', and within 'digitStruct', key 'name' and 'bbox' can be found, I used h5py API to read the file.

import h5py

f = h5py.File('train.mat')

print len( f['digitStruct']['name'] ), len(f['digitStruct']['bbox'] )

I can read the array, however when I loop though the array, how can I read each item?

for i in f['digitStruct']['name']:

print i # only print out the HDF5 ref

解决方案

Writing in Matlab:

test = {'Hello', 'world!'; 'Good', 'morning'; 'See', 'you!'};

save('data.mat', 'test', '-v7.3') % v7.3 so that it is readable by h5py

jEEOw.png

Reading in Python (works for any number or rows or columns, but assumes that each cell is a string):

import h5py

import numpy as np

data = []

with h5py.File("data.mat") as f:

for column in f['test']:

row_data = []

for row_number in range(len(column)):

row_data.append(''.join(map(unichr, f[column[row_number]][:])))

data.append(row_data)

print data

print np.transpose(data)

Output:

[[u'Hello', u'Good', u'See'], [u'world!', u'morning', u'you!']]

[[u'Hello' u'world!']

[u'Good' u'morning']

[u'See' u'you!']]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值