How do I call a MATLAB function in Python with the function output being a structure that contains a structure-array field?

I have written a MATLAB function which outputs a structure containing a field that is a structure array of more than 1 element. How can I call this function in Python using MATLAB Engine API?

 Accepted Answer

You cannot call this type of MATLAB function in Python using Engine API because of the structure array in the function output. This is a known limitation as of R2019a.
Please run the below command in the command window of installed MATLAB R2019a version to get release specific documentation:
>> web(fullfile(docroot, 'matlab/matlab_external/handle-data-returned-from-matlab-to-python.html'))
As a workaround, you may consider converting the structure array to a row or column cell arrays of structures. Here is an example MATLAB function that works with Engine API:
function out = demo(x)
% create a scalar structure: 'st'
st.a = ones(2);
st.b = x;
out.st = st; % THIS WILL WORK with MATLAB Engine API
% % create a structure array: 'sta'
% sta(1).c = ones(2);
% sta(1).d = '1';
% sta(2).c = ones(3);
% sta(2).d = '2';
% out.sta = sta; % THIS WILL NOT WORK with MATLAB Engine API
% create a row cell array of scalar structures : 'sta'
st1.c = ones(2);
st1.d = '1';
st2.c = ones(3);
st2.d = '2';
sta = { st1, st2 };
out.sta = sta; % THIS WILL WORK with MATLAB Engine API
end
Please follow the below link to search for the required information regarding the current release:

More Answers (0)

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!