Can't pass cell array with shape of [N, 1] to matlab from Python

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

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start with the Python MWE using start_instance, loadcase, workspace, and the MATLAB engine, then compare the native and round-trip bus_name sizes. Done means a nested [N, 1] cell array remains [N, 1] when passed from Python back to MATLAB and the runpf round trip succeeds.

Written by the indexing model from the issue text.

Description

Nested list of shape [N, 1] is passed as [1, 1], causing error array cell dimension. From matlab, cell array [N,1] is passed to Python as list [N], but send it back will make it into [1, N]. Nested [N, 1] from Python will be treated as [1, 1]

Python MWE:

from matpower import start_instance
m = start_instance(engine="matlab")

CASE_NAME = "case_RTS_GMLC.m"

m.eval(f"mpc_native = loadcase('{CASE_NAME}');", nargout=0)
print("MATLAB native class:", m.eval("class(mpc_native.bus_name)", nargout=1))
print("MATLAB native size :", m.eval("size(mpc_native.bus_name)", nargout=1))

mpc = m.loadcase(CASE_NAME)

bus_name = mpc["bus_name"]
print("Python type      :", type(bus_name))
print("First entry      :", bus_name[0])
print("First entry type :", type(bus_name[0]))
mpc["bus_name"] = np.atleast_2d(mpc["bus_name"]).tolist()

m.workspace["mpc_rt"] = mpc
print("Round-trip class   :", m.eval("class(mpc_rt.bus_name)", nargout=1))
print("Round-trip size    :", m.eval("size(mpc_rt.bus_name)", nargout=1))

m.eval("runpf(mpc_native);", nargout=0)  # success due to correct cell shape
m.eval("runpf(mpc_rt);", nargout=0)  # fail due to [[1.0,1.0]] array for bus_name

m.exit()
MATLAB native class: cell
MATLAB native size : [[73.0,1.0]]
Python type      : <class 'list'>
First entry      : ABEL
First entry type : <class 'str'>
Round-trip class   : cell
Round-trip size    : [[1.0,1.0]]
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.