Arrays of structs should be returned as a list of the struct objects

Open
#10 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
32/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
matlab, python
Domain
api, tooling

Research direction

No source file or test is named. Start by reproducing the MATLAB Engine cases with a = whos and a Simulink.BusElement array, then trace the conversion and ml.get/indexing entry points. Done means struct arrays become usable Python lists of struct objects and BusElement access no longer fails.

Written by the indexing model from the issue text.

Description

Currently, using Matlab 2019a and the associated Matlab Engine.

The engine fails to convert arrays of structs, which is a known limitation. However, the equivalent of a vector in Matlab is simply a list in Python, or, for a matrix, a list of lists. Other non-struct items correctly translate this way. The struct arrays should simply pass a list of struct objects to Python (which may need further conversion to be usable).

I worked around this by checking if it is a struct with max(size) greater than 1, and if so, I convert the variable to a cell with the struct2cell property, then pull the object into Python, which pulls it in as a list of the struct objects (or list of lists). The engine should be able to do this directly, rather than having to figure out whether to do a conversion first in the Matlab workspace.

An example of such an object is the output of whos: a = whos in Matlab.

Simulink.BusElement example and different error

Also, the Simulink.BusElement object of a Simulink.Bus will be a vector (though could be size 1). It will pull the multi-dimensional struct array into an object in Python, but the .get property fails, even if accessing the keys. i.e. ml.get(BusElement[0]) fails when BusElement is the matlab object given for the Elements property of a Simulink.Bus after translation into Python. The odd thing here is this fails with a different error. There seems to be a problem with the array indexing either in the struct array object, or the get function, or both.

Note

I just happened across the get and struct2cell properties by chance after a lot of digging from a stray answer for something else. I was pulling my hair out on getting this to work. Better documentation or examples for different types would be very helpful.

Dominant language
Python
Stars
97
Forks
13
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mathworks/matlab-engine-for-python

All issues in mathworks/matlab-engine-for-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.