I receiving error while running this code; Error in sector_selection (line 5) switch flag
Mostra commenti meno recenti
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 Commenti
Without knowing what the error was, I'm going to guess it had something to do with not enough arguments. You're probably calling the function with fewer than 4 arguments.
If that's not what it is, you'll have to reveal what the error was and describe how you're calling the function.
muhammad ghufran
il 29 Set 2021
Cris LaPierre
il 29 Set 2021
How do you call your function?
DGM
il 30 Set 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
il 30 Set 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.
Risposte (0)
Categorie
Scopri di più su General Applications in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!