I receiving error while running this code; Error in sector_selection (line 5) switch flag

9 views (last 30 days)
function [sys,x0,str,ts] = sector_selection(t,x,u,flag)
% The following outlines the general structure of an S-function.
%
switch flag
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end sfuntmpl
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes
%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes; %
sizes.NumContStates = 0; %
sizes.NumDiscStates = 0; %
sizes.NumOutputs = 1; %
sizes.NumInputs = 2; %
sizes.DirFeedthrough = 1; %
sizes.NumSampleTimes = 1; %
sys = simsizes(sizes); %
x0 = []; %
str = [];
ts = [-1 0]; %
% end mdlInitializeSizes
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
if(u(1)==0)
N=1; %
else
a1=u(1);
b1=u(1)*(-0.5)+(sqrt(3)/2)*u(2); %%
c1=u(1)*(-0.5)-(sqrt(3)/2)*u(2);
if a1>0
a=0;
else
a=1;
end
if b1>0
b=0;
else
b=1;
end
if c1>0
c=0;
else
c=1;
end
N=4*a+2*b+c; %
end
sys=N;
% end mdlOutputs
  5 Comments
DGM
DGM on 30 Sep 2021
If that's the error, then you're calling the function without specifying the fourth argument. If arguments are omitted in a function call, an error will be only be thrown if and when the function tries to use the missing argument.
Walter Roberson
Walter Roberson on 30 Sep 2021
You cannot run that function just by clicking on the green Run button. That function is intended to be invoked as an S Function from inside Simulink, with Simulink automatically passing it several parameters.
If you are trying to debug it, then you need to emulate Simulink's calling sequence, by going down to the command line and calling it and passing in appropriate parameters.

Sign in to comment.

Answers (0)

Categories

Find more on General Applications 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!