How to return numpy.array using python in simulink

조회 수: 8 (최근 30일)
hao wu
hao wu 2020년 9월 2일
댓글: hao wu 2020년 9월 7일
I built a neural network controller in python, and want to control a simulink model using the python function. The input and output of my python function are 'numpy.array'. The output shape is [4, 1].
I used a 'matlab function' to call the python function in simulink. The code of the matlab function is as follows:
function [u1, u2, u3, u4] = fcn(data_in)
coder.extrinsic('py.controller.multi_output')
out = 0;
out = py.controller.multi_output(data_in);
u1 = out(1);
u2 = out(2);
u3 = out(3);
u4 = out(4);
The 'controller' is the python code and the 'multi_output' is the name of my python function.
The 'py.controller.multi_output(data_in)' works well in matlab and returns ndarray. I can transform the ndarray to double.
But it does not work in simulink:
An error occurred while running the simulation and the simulation was terminated
Caused by:
MATLAB expression '<output of py.controller.multi_output>' is not numeric.
Why does this error occurs?
  댓글 수: 1
hao wu
hao wu 2020년 9월 7일
Create a separate MATLAB function (myfcn.m) that calls "py.controller.multi_output" and performs the conversion between MATLAB double arrays and the ndarrays for Python.
It works.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by