Skip to content

Commit 029c6a8

Browse files
committed
better logic on struct array conversions
1 parent 32c5bbd commit 029c6a8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

code/matlab_py/mat2py.m

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,25 @@
104104
case 'struct'
105105
x_py = py.dict();
106106
F = fieldnames(x_mat);
107-
for i = 1:length(F)
108-
if length(x_mat) == 1
109-
x_py.update(pyargs(F{i}, mat2py(x_mat.(F{i}))));
110-
else
111-
for j = 1:length(x_mat)
112-
x_py.update(pyargs(F{i}, mat2py(x_mat(j))));
107+
if (length(x_mat) > 1) && ...
108+
(class(x_mat) == "struct")
109+
% struct array
110+
x_py = py.list();
111+
for j = 1:length(x_mat)
112+
x_py.append( mat2py(x_mat(j)) );
113+
end
114+
else
115+
for i = 1:length(F)
116+
if (length(x_mat.(F{i})) > 1) && ...
117+
(class(x_mat.(F{i})) == "struct")
118+
% struct of struct array
119+
List = py.list();
120+
for j = 1:length(x_mat.(F{i}))
121+
List.append( mat2py(x_mat.(F{i})(j)) );
122+
end
123+
x_py.update(pyargs(F{i}, List));
124+
else
125+
x_py.update(pyargs(F{i}, mat2py(x_mat.(F{i}))));
113126
end
114127
end
115128
end

0 commit comments

Comments
 (0)