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

조회 수: 14 (최근 30일)
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
DGM
DGM 2021년 9월 30일
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 2021년 9월 30일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by