1
votes

I have trained the neural network in matlab script file and saved the trained data into a .mat file. So that after loading the .mat file in Simulink user defined functions, I could use this trained data to test the inputs. But I get run time error

Call to Matlab function aborted: Error calling Matlab function 'sim'. Press OK to open the debugger.

and in debugger the error is

MATLAB Function Interface Error: Error calling MATLAB function 'sim'. Block Neural Network Function (#108) While executing: none

The code is as

function [tau1p,tau2p]  = Nntwork(theta1,theta1d,theta2,theta2d,theta1dd,theta2dd)
coder.extrinsic('load');
coder.extrinsic('sim');
net=load('trainednet.mat');
a=zeros(1,2);
a=sim(net,[theta1;theta1d;theta1dd;theta2;theta2d;theta2dd]);

if some one could help me to resolve this error.

1

1 Answers

0
votes

The first thing I'd check is that your variable net is actually a NN object. The syntax that you are using for load loads all variables in the mat file into a structure called net, and you most likely need to use net.net to extract the net variable from the net structure. (Of course if that's the case then you shouldn't call the structure (i.e. the output from load) net as it will be very confusing.

However, I'd suggest the best way to do this is to wrap you code into another function, and call that function from the MATLAB Function Block. i.e.

Make the MATLAB function block something like:

function [tau1p,tau2p]  = NntworkWrapper(theta1,theta1d,theta2,theta2d,theta1dd,theta2dd)
coder.extrinsic('Nntwork');
a=zeros(1,2);
a=Nntwork([theta1;theta1d;theta1dd;theta2;theta2d;theta2dd]);

Then have this additional function in a separate m-file:

function a = Nntwork(theta_data)
load('trainednet.mat');
a=sim(net,theta_data);

A set-up like that will enable you to run and test the NN code independently of Simulink, but also call it from Simulink when required.