I am trying to write a level-2 S-function for use in system identification live during a simulation using the n4sid function. However, it does not appear that the output of the n4sid function, an idss datatype, is supported in the output. Is there a way to output this data type from an S-function? Alternatively, can this data type be used internally in an S-function and fed directly into another Matlab function inside the S-function code? Thanks.
Is there a way to output this data type from an S-function?
No.
Alternatively, can this data type be used internally in an S-function and fed directly into another Matlab function inside the S-function code?
Yes.
You can create data/destroy data/call functions etc just like any other piece of MATLAB code.
Related
I have a StateFlow chart (C action language) that calls a Matlab function embedded on it. This Matlab function calls an external C function using coder.ceval, which returns a variable that is declared using coder.opaque.
I have another Matlab function embeeded on the same Stateflow that calls another external C function and that requires the coder.opaque variable returned by the previous Matlab function to be passed as an argument.
Is there a way that I can pass that coder.opaque variable from the first Matlab function block to the second one?
I would imagine that joining the two Matlab functions inside the same block (and adding a switch-case statement to execute one or the other) can work, but I would really like to have them in separate blocks.
My objective is to generate the code of the model. Thanks in advance!
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.
i can't find a way to use the same matlab function in more Simulink block. I used function block but in this way each time a have to copy all code changes. What is the best way to this?
Create a file named <function name>.m that contains your function, then have the function blocks have one line that simply invokes that function.
What is the block that I can use for the creating a custom s-function in simulink?
I want to use C code using S-Function.. I know that there are functions that I have to define but I don't know how to reach it or which code to start with exactly..
Note: I am totally new to matlab and simulink..
Check out the S-function Builder block from Simulink's library (in Simulink->User Defined Functions). You can set number of states, inputs, outputs and also add code for calculating the derivatives + outputs. When you press Build it generates C code and you can start with that as a template. Or you could use the S-function Builder interface for writing all your code.
Edit: Matlab also has a built-in template that you can access:
edit([matlabroot,'/simulink/src/sfuntmpl_basic.c']);
Did you check the MATLAB documentation on this?
It is usually pretty complete, once you find the right page to read.
For s-functions there e.g. is
http://www.mathworks.de/de/help/simulink/sfg/how-to-implement-s-functions.html
which includes links to example s-functions you can find in your matlab-installation.
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.