필터 지우기
필터 지우기

Attempt to extract field 'addgivenNumbers' from 'mxArray'.

조회 수: 8 (최근 30일)
Rifshu Hussain Shaik
Rifshu Hussain Shaik 2022년 2월 18일
답변: Shivang 2023년 9월 29일
Hi, I am facing an error when i am trying to call python function in simulink.
how to fix this error? and thank you
heres the python code saved as 'add.py' :
def addgivenNumbers( num1, num2):
sum = num1 + num2 + 20
return sum
Simulink Matlab function code:
function a = fcn(num1, num2)
a = 0;
coder.extrinsic('py.importlib.import_module');
coder.extrinsic('py.importlib.reload');
mod = py.importlib.import_module('add');
a = mod.addgivenNumbers(num1, num2);
end

답변 (1개)

Shivang
Shivang 2023년 9월 29일
Hi Rifshu,
I understand you’re running into an error while calling a user-defined Python function from inside a MATLAB Function block.
In your code, the variable 'mod', which stores the value returned by the Python function 'import_module', is an mxArray. Attempting to call a function in the module using the dot operator generates an error, as this operation is not supported with mxArrays.
This can be avoided by directly calling the 'addgivenNumbers' function, after declaring it as extrinsic:
coder.extrinsic('py.add.addgivenNumbers');
a = py.add.addgivenNumbers(num1,num2);
You should not encounter any errors now.
Hope this helps!
-Shivang

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by