首页 > 解决方案 > matlab coder.extrinsic,定义/初始化更复杂的数据类型

问题描述

在 Simulink 和 coder.extrinsic 中使用 matlab 系统模块。在此链接中,在“使用 mxArrays”标题下,它说 y 必须定义为双精度标量才能使代码正常工作,因为该函数将返回双精度标量。

但是更复杂的数据类型,比如 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');

有什么建议么?

标签: matlabsimulink

解决方案


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

%--- 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()

推荐阅读