# How to create variable system in S-Function using feedback data from simulink or workspace)

8 views (last 30 days)
moha on 29 Jul 2014
Answered: Kaustubha Govind on 27 Aug 2014
Hello
if true
function [sys,x0,str,ts] = mycsfunc(t,x,u,flag)
% x' = Ax + Bu
% y = Cx + Du
%
% Generate a continuous linear system:
A=[-1 -10
1 0];
B=[ 1 -20
0 -2];
C=[ 9 2
1 -5];
D=[-14 0
1 3];
%
% Dispatch the flag.
%
switch flag,
{1,3,9}
...
..
.
%
End of mycsfunc.
%==============================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the
% S-function.
%==============================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D)
%
% Call simsizes for a sizes structure, fill it in and convert it
% to a sizes array.
%
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1; % Matrix D is nonempty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
...
..
.
End of mdlOutputs.
end
In simulink I have a variable prameters that can effect my system parameters. K is a parameter in simulink or workspace that can change continuously.How can I define this parameter to my s-function to get K , P data from simulink or workspace to create variable system like below in S-Function:(How to create variable parameter dependent system in S-Function as below)
if true
A=[-1 -P
1 0];
B=[ 1 -20
0 -2];
C=[ 9 2
1 -5];
D=[-K 0
1 3];
end
Thanks

Kaustubha Govind on 27 Aug 2014
You can simply add additional parameters to the main function as follows:
function [sys,x0,str,ts] = mycsfunc(t,x,u,flag,P,K)
and use these within the function and/or pass them onto to mdlInitializeSizes, mdlOutputs, etc.
On the S-function block, enter values for P and K (in that order) in the "S-function parameter" edit-box. These values can be constant numbers or mask/workspace variables, just like for other block parameters.
Also, note that you are using Level-1 S-functions which have been deprecated for several years and have a lot of limitations. I would recommend that you convert to Level-2 S-functions if possible.

### Categories

Find more on Create MATLAB S-Functions in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!