Simulink:如何在嵌入式 matlab 函数中使用工作区中的对象实例?
在本例中,我的基础工作区中有一个神经网络 (NN) 实例,我希望将其用于 Simulink 仿真。我将 NN 的使用封装在一个嵌入式 Matlab 函数中,并带有应由网络使用的输入参数。
原则上我希望做这样的事情:
function XBDDprime = NN(F, XB, XBD, XBDD)
%#eml
global net;
XBDDprime = net([F XB XBD XBDD]');
目标是从基础工作区获取net
对象(它是network
类的一个实例)。
这是我使用 evalin
从工作区读取变量的问题:
function XBDDprime = NN(F, XB, XBD, XBDD)
%#eml
eml.extrinsic('evalin');
net = evalin('base', 'net'); %Fetch net from workspace
XBDDprime = net([F XB XBD XBDD]'); %Error!
这无法编译,因为 simulink 似乎认为 net 是一个数组,而 net(...)< /code> 是数组下标(实际错误消息:不支持对 mxArray 进行下标)。
在我看来,Simulink 需要对用于编译嵌入式 matlab 函数的任何对象有完整的定义,对吗?有解决办法吗?我可以使用 Simulink.Signal 以某种方式包装 NN 并将其作为参数添加到功能块吗?
编辑
我也尝试使用 load
从文件加载序列化的 net
对象。那也没用。编译器认为 s
是 mxArray
似乎是同样的问题。
function XBDDprime = NN(F, XB, XBD, XBDD)
%#eml
eml.extrinsic('load')
s = load('net');
XBDDprime = s.net([F XB XBD XBDD]');
解决方案
我最终放弃了并选择了 matlab 功能块,它看起来像上面的任何示例。
In this case I have a neural network (NN) instance in my base workspace that I wish to use in a simulation with Simulink. I wrapped the use of the NN in an Embedded Matlab function with input arguments that should be used in by the net.
In principal I wish to do something like this:
function XBDDprime = NN(F, XB, XBD, XBDD)
%#eml
global net;
XBDDprime = net([F XB XBD XBDD]');
Where the goal is to fetch the net
object from base workspace (which is an instance of the class network
).
This a swing at the problem where I used evalin
to read the variable from workspace:
function XBDDprime = NN(F, XB, XBD, XBDD)
%#eml
eml.extrinsic('evalin');
net = evalin('base', 'net'); %Fetch net from workspace
XBDDprime = net([F XB XBD XBDD]'); %Error!
This doesn't compile because it seems like simulink thinks net is an array and net(...)
is array subscripting (actual error message: Subscripting into an mxArray is not supported).
It seems to me like Simulink needs to have a full definition of any object used to be able to compile the embedded matlab function, is that correct? Is there even a solution? Can I use Simulink.Signal
somehow to wrap the NN and add that as an argument to the function block?
Edit
I tried using load
as well to load the serialized net
object from file. That didn't work either. Seems to be the same problem where the compiler thinks s
is an mxArray
.
function XBDDprime = NN(F, XB, XBD, XBDD)
%#eml
eml.extrinsic('load')
s = load('net');
XBDDprime = s.net([F XB XBD XBDD]');
Solution
I finally caved and went for the matlab function block which can look like any of the examples above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
net
参数定义为NN
函数的输入,并使用From Workspace
块将其放入模型中。我不确定这是否适用于Embedded MATLAB function
块,您可能需要切换到M Code
块。You could define the
net
parameter as an input of theNN
function and use aFrom Workspace
block to get it into your model. I'm not sure if this will work with anEmbedded MATLAB function
block, you might need to switch to anM Code
block.生成用于神经网络仿真的 Simulink 模块
句法
gensim(净,st)
寻求帮助
键入帮助网络/gensim。
Generate Simulink block for neural network simulation
Syntax
gensim(net,st)
To Get Help
Type help network/gensim.