MATLAB Function Interface Error - matlab

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.

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.

Related

How can I call a m file script into simulink block which uses simout file?

I have a simulink file and I'm working on designing a controller.
The file contains several electronics converters connected in parallel and each converter has a controller. The converter measures voltage and current and pass them to the controller to control.
The simulink file generates the simout file after simulation and that simout file is being used to calculate the impedance of the electrical grid in a m script file as a transfer function. I need to use the transfer function of impedance from the workspace in the place of " Req +sLeq" as in the purple marked position of the picture.
In other words, in my controller design, I need to call that m-file transfer function into a block and use the transfer function (impedance) as active damping during the simulation.
I'm not sure how can I call the transfer function from the m file (generated from simout) back to the simulink block during simulation.
The simulation need to be done in one go as closed loop system.
Please help me for this.
I can think of two ways to solve this. First, you can try to use a Matlab function block:
https://www.mathworks.com/help/simulink/slref/matlabfunction.html
Otherwise, you can always split your routines. Split your simulations, obtain your transfer function -which now exists in the workspace- then run your control system simulation using a separate file.
Dummy main-file
simOut = sim('electricalSimulation'); % Your first simulation
output = simOut.output;
input = simOut.input;
electricalTf = obtainTF(output, input); % Your identification routine
sim('completeSystem');

How to call out sim in matlab function block

I am trying to run a PID controller (another model in simulink) using sim command in a matlab function block.
However it takes long iteration, seems to be that the matlab function block need more time.
How to get faster result?
Here is my code in matlab function block:
function [Kpp,Kii,Kdd] = fcn(u)
coder.extrinsic('sim');
sim('ExternePID'); %call out to ExternePID model
Don't do that, that won't work. Instead include your second Simulink model as a referenced model in your top-level Simulink model. This way, both models can run concurrently.

How can I get workspace variables in MATLAB Function?

I am using Matlab function in my simulink code where I am using the load command for getting some matrices and variables from the workspace
persistent ProblemParams;
if isempty(ProblemParams)
ProblemParams = load('ProblemParams.mat');
end
This is working well, however there can be problem when I am running multiple simulations at the same time, hence I would like to know what other options do I have to pass an array to this block from MATLAB workspace?
Whether or not the above works, it's not the right way to get data into the block. You should load the variable into the MATLAB Workspace prior to starting the simulation, then pass the variable into the MATLAB Function Block as a Parameter Argument.

Problems with Embedded Functions within Simulink

I'm trying to simulate a very simple model using an embedded matlab function that takes the input and add's 10 to the value using a constant block that inputs into the matlab function, which then outputs to a display block.
As soon as I press simulate I get an abundance of errors. First I get a huge paragraph in orange text stating a warning out the solver 'variableStepDiscrete' instead of solver 'ode45'
Here is the remaining lines that are echo'd from the command prompt:
Code Directory :
"/Users/dazgti/Documents/MATLAB/slprj/_sfprj/embeddedFunction/_self/sfun/src"
Machine (#32): "embeddedFunction" Target : "sfun"
Chart "MATLAB Function" (#49):
.
"c2_embeddedFunction.h"
"c2_embeddedFunction.c"
"embeddedFunction_sfun.h"
"embeddedFunction_sfun.c"
"embeddedFunction_sfun_debug_macros.h"
Interface and Support files:
"embeddedFunction_sfun_registry.c"
Code generation failed Attempt to execute SCRIPT union as a function:
/Users/dazgti/Documents/MATLAB/union.m
I have a script file within my matlab directory called union.m, but I have no idea why its mentioning it.
function y = fcn(u)
%#codegen
x = u + 10;
y = x;
MATLAB Function block works by generating "C" code for the MATLAB code you entered in the block. In the process of generating code there could have been a call to union function in MATLAB from MATLAB Function block infrastructure. Since you have overridden the union function instead of the built-in function MATLAB might have attempted to call your script which caused the error. It is better to avoid naming your functions same as MATLAB built-in functions.

Matlab Simulink function

I am building a reduced order observer in MATLAB. The matrices are calculated using functions/script files outside matlab and simulink function blocks are using these functions to calculate values.
The problem is that some commands like 'acker', 'place' etc which used to work on command window/function/script files are not working in simulink function block and showing errors.
I tried using simin block to take these matrices from workspace but it is also showing errors which I can't understand.
Thanks for your help.
If I get your question correctly then, from User defined functions, you could add a Matlab function block with the following code:
function fcn(in)
%#codegen
coder.extrinsic('acker', 'place')
# Now you can use acker, place so add more code.