簡體   English   中英

如何在simulink中使用python返回numpy.array

[英]How to return numpy.array using python in simulink

我用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.

有沒有人告訴我如何修復它?

我建議創建一個單獨的 MATLAB 函數 (myfcn.m),它調用“py.controller.multi_output”並執行 MATLAB 雙數組和 Python 的 ndarrays 之間的轉換。

然后,您可以在 Simulink 內的 MATLAB Function Block 中將該函數聲明為外部函數,然后簡單地調用它。

問題基本上是 MATLAB 函數塊中的任何內容(對外部函數的調用除外)都需要 MATLAB 子集支持以進行代碼生成。 ndarrays 不受支持,因此它們不能在 MATLAB Function 模塊本身內部使用,但是它可以在外部函數內部使用,只要該函數具有 Simulink(MATLAB 代碼生成子集)支持的輸入/輸出參數)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM