Bog*_*dan 3 python file-io matlab
所以让我们开始说我是matlab的初学者.我正在使用python,现在我收到了matlab文件中的一些数据,我需要将其导出为可以与python一起使用的格式.
我google了一下,发现我可以使用以下方法将matlab变量导出到文本文件:
dlmwrite('my_text', MyVariable, 'delimiter' , ',');
现在我需要导出的变量是16000 x 4000矩阵的双精度格式0.006747668446927.现在问题出现在这里.我需要导出每个double的完整值.尝试使用该功能可以让我以格式导出数字0.0067477.这是行不通的,因为我需要更多的精确度来做我正在做的事情.那么如何导出每个变量的完整值呢?或者如果你有更优雅的方式在python中使用那个巨大的matlab矩阵,请随意.
此致,波格丹
为了在Python和Matlab之间交换大块的数值数据,我推荐使用HDF5
Python绑定称为h5py
以下是两个方向的两个示例.首先从Matlab到Python
% matlab
points = [1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12 ];
hdf5write('test.h5', '/Points', points);
# python
import h5py
with h5py.File('test.h5', 'r') as f:
    points = f['/Points'].value    
现在从Python到Matlab
# python
import h5py
import numpy
points = numpy.array([ [1., 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ])
with h5py.File('test.h5', 'w') as f:
    f['/Points'] = points
% matlab
points = hdf5read('test.h5', '/Points');
注意 Matlab中的列将在Python中作为一行出现,反之亦然.这不是一个错误,而是C和Fortran解释内存中连续数据的方式之间的差异.
| 归档时间: | 
 | 
| 查看次数: | 3829 次 | 
| 最近记录: |