Reading a Matlab's cell array saved as a v7.3 .mat file with H5py

后端 未结 3 1160
不思量自难忘°
不思量自难忘° 2020-12-31 21:12

I saved a cell array as a .mat file in Matlab as follows:

test = {\'hello\'; \'world!\'};
save(\'data.mat\', \'test\', \'-v7.3\')

How can I

3条回答
  •  有刺的猬
    2020-12-31 21:25

    This answer should be seen as an addition to Franck Dernoncourt's answer, which totally suffices for all cell arrays that contain 'flat' data (for mat files of version 7.3 and probably above).

    I encountered a case where I had nested data (e.g. 1 row of cell arrays inside a named cell array). I managed to get my hands on the data by doing the following:

    # assumption:
    # idx_of_interest specifies the index of the cell array we are interested in
    # (at the second level)
    
    with h5py.File(file_name) as f:
        data_of_interest_reference = f['cell_array_name'][idx_of_interest, 0]
        data_of_interest = f[data_of_interest_reference]
    

    Reason this works for nested data: If you look at the type of the dataset you want to retrieve at a deeper level, it says 'h5py.h5r.Reference'. In order to actually retrieve the data the reference points to, you need to provide that reference to the file object.

提交回复
热议问题