matlab coder.extrinsic, 定义/初始化更复杂的数据类型

matlab coder.extrinsic, define / initialize more complex data type

在 Simulink 和 coder.extrinsic 中使用 matlab 系统块。在 this link 中,在“使用 mxArrays”标题下,它说 y 必须定义为双精度标量才能使代码工作,因为该函数将 return 是双精度标量。

但是更复杂的数据类型,例如 python 模块呢?澄清一下:

% foo(x) returns a double scalar

coder.extrinsic('foo', 'py.importlib.import_module');
y = 0; % define y as double scalar
y = foo(x); % works fine

np =  ??? ; % how do I define np so that the row below will work? 
np = py.importlib.import_module('numpy');

有什么建议吗?

这里的最佳做法是将所有内容都放在同一工作目录下的 matlab (example.m) 函数下,将所有 python 调用和变量放在其中,然后从Simulink 功能块。这样你就不会 运行 陷入那些可怕的定义错误:

%--- example.m file (exists in the working directory)
function y = example(x)
  y = foo(x);  
  d = py.np.array() 
  %all python functions go here
end

% ---- inside the Simulink Matlab Function Block
coder.extrinsic('example');
out = 0; %preallocate the output
out = example()