Skip to content

Commit

Permalink
support None, byte arrays, Python numbers as dictionary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Jun 13, 2022
1 parent 00aefd5 commit b7182d1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions code/matlab_py/py2mat.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@
key_index = int64(0);
for key = cell(py.list(x_py.keys()))
key_index = key_index + 1;
v_name = string(key);
if class(key) == 'cell'
% key could be a Python number
v_name = string(py.str(key{1}));
else
v_name = string(key);
end
if isvarname(v_name)
x_mat.(v_name) = x_py.get(v_name);
else
% this key can't be used; replace it
fixed = sprintf('K%06d_', key_index) + ...
regexprep(string(key),'\W','_');
regexprep(v_name,'\W','_');
x_mat.(fixed) = x_py.get(v_name);
end
end
Expand Down Expand Up @@ -170,6 +175,12 @@
case 'logical'
x_mat = logical(x_py);

case 'py.bytes'
x_mat = uint8(x_py);

case 'py.NoneType'
x_mat = '';

% punt
otherwise
% return the original item? nothing?
Expand Down

0 comments on commit b7182d1

Please sign in to comment.