【问题标题】:How to return numpy.array using python in simulink如何在 simulink 中使用 python 返回 numpy.array
【发布时间】:2020-09-02 13:14:58
【问题描述】:

我用python构建了一个神经网络控制器,想用python函数控制一个simulink模型。我的 python 函数的输入和输出是'numpy.array'。输出形状为 [4, 1]。 我使用“matlab 函数”在 simulink 中调用 python 函数。 matlab函数代码如下:

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);

'py.controller.multi_output(data_in)' 在 matlab 中运行良好并返回 ndarray:

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

  Python ndarray:

     1
     2
     3
     4

我可以将 ndarray 转换为双倍。 但它在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.

有人告诉我怎么解决吗?

【问题讨论】:

    标签: python matlab numpy simulink


    【解决方案1】:

    我建议创建一个单独的 MATLAB 函数 (myfcn.m) 来调用 "py.controller.multi_output" 并执行 MATLAB 双精度数组和 Python 的 ndarray 之间的转换。

    然后,您可以在 Simulink 内的 MATLAB Function Block 中将该函数声明为外部函数,然后直接调用它。

    问题基本上是 MATLAB 功能块中的任何内容(对外部函数的调用除外)都需要 MATLAB 子集支持以进行代码生成。不支持 ndarrays,因此它们可能不会在 MATLAB Function 模块本身内部使用,但是它可以在外部函数内部使用,只要该函数具有 Simulink 支持的输入/输出参数(MATLAB 代码生成子集) )。

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2014-07-12
      • 2019-08-16
      • 2012-06-27
      • 1970-01-01
      • 2021-01-24
      相关资源
      最近更新 更多