0

Suppose data, saved in .mat file needs to be annotated or the meta-data of some file need to be stored:

x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

mdic = {"data": x,
        "classes": {'name_of_class_1': 0,
                    'name_of_class_2': 1
                   }
       }

When I save it with scipy.io.savemat() and load it back with scipy.io.loadmat I got not quite readdable structure:

{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri Jan 20 16:52:17 2023',
 '__version__': '1.0',
 '__globals__': [],
 'data': array([[1, 2, 3],
        [4, 5, 6]], dtype=int32),
 'classes': array([[(array([[0]]), array([[1]]))]],
       dtype=[('name_of_class_1', 'O'), ('name_of_class_2', 'O')])}

Is there a better way to store dict of dict/jsons in .mat files?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Michael
  • 339
  • 2
  • 11
  • 1
    MATLAB doesn't have `dict`. `savemat` makes a MATLAB `struct`, which `losdmat` returns as a `structured array`. Try `data['classes']['name_of_class_1'].item()`. Test that expression piece by piece if it doesn't make sense. And read up on structured arrays. – hpaulj Jan 20 '23 at 18:08

0 Answers0