using level 1 S-function with simulink

Hi all,
I am modeling a separator model with pressure and water level as 2 variables. I first made an m-file(hamda.m) with ode23tb, which is working( 4 plots: pressure, water level, flow of liq output and flow of gas output. then I used an level 1 s-function( gabileh) applying the m-file(hamda.m) I created as fellow
function [sys,x0,str,ts,simStateCompliance] = gabileh(t,x,u,flag,varargin)
% M-File S-Function implementing
QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes(t,x,u,flag);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys = mdlDerivatives(t,x,u,QLi,Qgi,kv1,kv2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update, Output, and Terminate %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case {2, 4, 9},
sys = []; % do nothing
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3
sys = mdlOutputs(t,x,u);
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
%
%===================================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%===================================================================================
%
function [sys, x0,str,ts] = mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 4;
sizes.NumInputs = 4;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0]; % continuous sample time: [period, offset]
% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < The default setting; warn and assume DefaultSimState
simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
%
%===================================================================================
% mdlDerivatives
%
%===================================================================================
%
function sys = mdlDerivatives(t,x,u,varargin)
QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);
sys = hamda(t,x,u);
% end mdlInitializeSizes
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%
function sys = mdlOutputs(t,x,u)
sys =Qgt;
sys =QLt;
sys = x;
% end mdlOutputs
when I used the level-1 s-function in a simulink with 4 inputs and 4 ouputs, I keep having the same error:
Error in 'seprig/M-file S-Function1' while executing M-File S-function 'gabileh', flag = 0 (initialize), at start of simulation. MATLAB error message:
Attempted to access u(1); index out of bounds because numel(u)=0.
please help, thanks in adavance

Answers (1)

The inputs to the S-function are not available at the time that mdlInitializeSizes is called (u is an empty variable - you could set a breakpoint and verify that). If you think about it, the stage flag=0 is the setup stage, so no inputs are available at this point. So the line:
QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);
errors out.
Move it under the "case 1" section, so it is executed only where available and required.

4 Comments

Thanks Kastuba,
I tried and it gives this error:
Error in 'seprig/M-file S-Function1' while executing M-File S-function 'gabileh', flag = 0 (initialize), at start of simulation. MATLAB error message:
Too many input arguments.
thanhs
You function mdlInitializeSizes has no input arguments. Change:
[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes(t,x,u,flag);
to
[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes;
Thanks Kastuba,
I have steel the same problem, even with the changes
function [sys,x0,str,ts] = gabileh(t,x,u,flag,varargin)
% M-File S-Function implementing
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts] = mdlInitializeSizes(varargin{:});
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys = mdlDerivatives(t,x,u,flag);
u(1)=QLi; u(2)=Qgi; u(3)=kv1; u(4)=kv2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update, Output, and Terminate %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case {2, 4, 9},
sys = []; % do nothing
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3
sys = mdlOutputs(t,x,u);
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
%
%===================================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%===================================================================================
%
function [sys, x0,str,ts] = mdlInitializeSizes(varargin)
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 4;
sizes.NumInputs = 4;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0,0];
str = [];
ts = [0 0]; % continuous sample time: [period, offset]
% end mdlInitializeSizes
%
%===================================================================================
% mdlDerivatives
%
%===================================================================================
%
function sys = mdlDerivatives(t,x,u,varargin)
sys = hamda(t,x,u);
% end mdlInitializeSizes
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%
function sys = mdlOutputs(t,x,u)
sys(1,2) = hamda(t,x,u);
sys(3,4) = x;
% end mdlOutputs
Simulink error message:
Error in 'seprig/M-file S-Function1' while executing M-File S-function 'gabileh', flag = 3 (output), at time 0. MATLAB error message:
Too many input arguments.
Thanks for ur help
You should perhaps run "dbstop if all error" and then run the Simulink model so that MATLAB automatically stops when an error occurs. You can debug the source of the error.

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Asked:

on 26 Sep 2011

Community Treasure Hunt

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

Start Hunting!