Pythonデータとmatlabデータ変換

5734 ワード

http://blog.csdn.net/landiaoxinqing/article/details/48622057
1.Pythonデータとmatlabデータ変換
1.1 Pythonロードと書き込み.matファイル
File IO(scipy.io)
See also
numpy-reference.routines.io (in numpy)
  • 1
  • 1
  • MATLAB files
    *loadmat(file_name[, mdict, appendmat])*    Load MATLAB file
    *savemat(file_name, mdict[, appendmat, ...])*   Save a dictionary of names and arrays into a MATLAB-style .mat file.
    *whosmat(file_name[, appendmat])*   List variables inside a MATLAB file
  • 1
  • ,
  • 1
  • ,
  • How to do
    import scipy.io as sio
    mat_contents = sio.loadmat('octave_a.mat')
    mat_contents
    >>>{'a': array([[[  1.,   4.,   7.,  10.],
            [  2.,   5.,   8.,  11.],
            [  3.,   6.,   9.,  12.]]]),
     '__version__': '1.0',
     '__header__': 'MATLAB 5.0 MAT-file, written by
     Octave 3.6.3, 2013-02-17 21:02:11 UTC',
     '__globals__': []}
    
    oct_a = mat_contents['a']
    oct_a
    >>>array([[[  1.,   4.,   7.,  10.],
            [  2.,   5.,   8.,  11.],
            [  3.,   6.,   9.,  12.]]])
    
    oct_a.shape
    >>>(1, 3, 4)
    
    sio.savemat('np_vector.mat', {'vect':vect})
  • 1
  • ,
  • 4
  • 5
  • ,
  • ,
  • 8
  • 9,
  • 10
  • 11
  • 15
  • ,
  • ,
  • ,
  • ,
  • ,
  • 1
  • ,
  • 4
  • 5
  • ,
  • ,
  • 8
  • 9,
  • 10
  • 11
  • 15
  • ,
  • ,
  • ,
  • ,
  • ,
  • If you want to inspect the content of a MATLAB file without reading the data into memory、use the whosmand:
    sio.whosmat('octave_a.mat')
    >>>[('a', (1, 3, 4), 'double')]
  • 1
  • 1
  • 高バージョンの.matデータの読み書きには、別のインターフェースが必要です。
    import h5py
    datasets = F:/MuraDefectData_6X6_10W_NonZCA_Batches.mat'
    
    f = h5py.File(datasets, 'r')
    TotalBatchImg = f['TotalBatchImg']
    TotalBatchImg = np.array(TotalBatchImg)
    wSize = f['wSize']
    wSize = np.array(wSize)