0

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);
u5 = out(4);

The 'py.controller.multi_output(data_in)' works well in matlab and returns ndarray:

out = py.controller.multi_output(data_in)
out = 

  Python ndarray:

     1
     2
     3
     4

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.

Does anyone tell me how to fix it?

haowu
  • 1
  • 1

1 Answers1

0

I would suggest to 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.

Then you can declare that function as extrinsic in the MATLAB Function Block inside Simulink and simply call that instead.

The problem is basically that anything in a MATLAB Function Block (except calls to extrinsic functions) will need to be supported by the MATLAB subset for Code Generation. ndarrays are not supported for this, so they may not be used inside the MATLAB Function block itself, however it may be used inside an extrinsic function, as long as that function has input/output arguments that are supported in Simulink (MATLAB Code Generation subset).

jorik
  • 131
  • 4