mandrake
mandrake

Reputation: 1253

Simulink: How can I use object instances from workspace in an embedded matlab function?

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.

Upvotes: 2

Views: 3782

Answers (2)

Mohsen
Mohsen

Reputation: 1

Generate Simulink block for neural network simulation Syntax gensim(net,st) To Get Help Type help network/gensim.

Upvotes: -2

AVH
AVH

Reputation: 11516

You could define the net parameter as an input of the NN function and use a From Workspace block to get it into your model. I'm not sure if this will work with an Embedded MATLAB function block, you might need to switch to an M Code block.

Upvotes: 1

Related Questions