Reading python pickle of dict in MATLAB causes error

I have a pickle file of a python dictionary whose keys are tuples `(10, 11), (10, 12), (11, 10), (11, 12), (12, 10), (12, 11)`.
I use the following commands to read them
fid = py.open('file.pickle','rb');
data = py.pickle.load(fid);
At this point, I think the code is working correctly as
data.keys()
in MATLAB gives the correct answer as
dict_keys([(10, 11), (10, 12), (11, 10), (11, 12), (12, 10), (12, 11)])
The equivalent of dict in python is structures ([1]) so when I try to convert it to `struct` using
data=struct(data)
, I get an error as
Error using py.dict/struct
Field names must be non-empty character vectors or a string
scalars.
What does the error mean here?

4 件のコメント

Mohammad Sami
Mohammad Sami 2021 年 3 月 11 日
According to matlab documentation, the fieldnames for structure have the following requirements, which your data does not meet.
field — Field name
character vector | string scalar
Field name, specified as a character vector or string scalar. Valid field names begin with a letter, and can contain letters, digits, and underscores. The maximum length of a field name is the value that the namelengthmax function returns.
BABA CHOUDHURY
BABA CHOUDHURY 2021 年 3 月 11 日
@Mohammad Sami where exactly is the error and is the problem related to writing in python or reading in matlab?
Mohammad Sami
Mohammad Sami 2021 年 3 月 11 日
Based on the output you have shown in your question, the keys are numeric values. As stated in the documentation matlab struct does not support these as field names. (see valid fieldname criteria)
Mohammad Sami
Mohammad Sami 2021 年 3 月 11 日
You can see this answer on how you can convert your dictionary into a Matlab type containers.Map.

サインインしてコメントする。

回答 (1 件)

Asvin Kumar
Asvin Kumar 2021 年 3 月 15 日

0 投票

Map containers might be what you're looking for.

カテゴリ

質問済み:

2021 年 3 月 11 日

回答済み:

2021 年 3 月 15 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by