How to get Simulink.Bus Elements in Python with matlab.engine

5 次查看(过去 30 天)
I'm having trouble with the syntax for getting Simulink.Bus elements in Python using matlab.engine. This is using Matlab 2019a.
I'm getting most variables and functions with work with matlab.engine, but the "get" command is giving me an error when trying to resolve the .Element objects of a bus with more than one element.
I can pull in the Bus okay to Python (i.e. m.workspace['<bus_name>']) and resolve it with get, but then when I pull out the Elements property, which is a vector, and try to resolve it, I get an error.
% Python
m = matlab.engine.start_matlab()
bus = m.workspace['bus_name']
% bus output in VS Code when debugging:
% <matlab.object object at 0x000001AF13F9CDF0>
% size:(1, 1)
bus_value = m.get(bus)
% bus_value output in VS Code:
% 'Alignment':-1.0
% 'Elements':<matlab.object object at 0x000001AF13F9C850>
% 'Description':''
% 'DataScope':'Auto'
% 'HeaderFile':''
% len():5
elements = bus_value['Elements']
% elements in VS Code:
% <matlab.object object at 0x000001AF1470D030>
% size:(2, 1)
This is all correct and matches what is in Matlab. I have 2 Bus Elements, so the Elements property is a struct with an array size of [2 1].
This is where I am stuck. In Matlab, if I do the same:
elements = <bus_name>.Elements
2×1 BusElement array with properties:
Min
Max
DimensionsMode
SampleTime
Description
Unit
Name
DataType
Complexity
Dimensions
% and then
elements(1)
BusElement with properties:
Name: '<some_element_name>'
Complexity: 'real'
Dimensions: 1
DataType: 'Bus: <some_bus_name>'
Min: []
Max: []
DimensionsMode: 'Fixed'
SampleTime: -1
Unit: ''
Description: ''
And elements(2) is similar for the other bus element.
And using get in Matlab generates an error on the Elements array:
>> get(elements)
% Error using Simulink.BusElement/get
% To call 'get(h)' with a single input argument, either h must be scalar or there
% must be an output argument.
And I get the same error in Python. But indexing into the elements array works in Matlab:
>> get(elements(1))
Min: []
Max: []
DimensionsMode: 'Fixed'
SampleTime: -1
Description: ''
Unit: ''
Name: 'inc_dbm_failed'
DataType: 'Bus: <some_bus>'
Complexity: 'real'
Dimensions: 1
But this fails in Python (Note that elements[1] in Python should be elements(2) in Matlab, as Python is zero-based indexing and Matlab is 1-based indexing)
e1 = m.get(elements[1])
% Traceback (most recent call last):
% File "<string>", line 1, in <module>
% File "d:\code\mca_app\app_container_inspector\.venv\lib\site-packages\matlabengineforpython-2018.1.0-py3.7.egg\matlab\engine\matlabengine.py", line 66, in __call__
% out=_stdout, err=_stderr)
% IndexError: number of indices should be equal to the number of dimensions
Is this a defect in the matlab engine? Or is there any other way to pull the element items into Python without extracting them into a variable in the matlab workspace first?

回答(1 个)

Shreeya
Shreeya 2023-9-18
I understand that you are receiving the below listed errors when trying to get the Simulink.Bus elements in Python using MATLAB’s python engine API.
  • “get(elements)” returns an error regarding the type of argument passed to the “get” function in MATLAB and python.
  • Indexing into “elements” and passing it as an argument to the “get” function results in an error in Python.
“get” function takes in a graphics object as an argument and returns its properties and property values. “elements” is a “2*1 BusElement array”. Since the “get” function expects an input argument of type graphics object, passing in an array might be causing the error. For the same reasons, passing “elements(1)” works perfectly.
Kindly refer to the documentation linked below for more details: https://www.mathworks.com/help/matlab/ref/get.htm
“get(elements[1])” is returning an indexing error in Python which does not seem related to the MATLAB’s Python engine API. You can check the dimensions of the “elements” variable and index it accordingly.
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by