ezekyal 发表于 2009-3-25 10:59

运行S函数出现 flag=2 call must be a real vector of length 0错误

运行下面这个S函数,出现这种错误:
Discrete state(s) returned by S-function 'dtc' in 'untitled1/S-Function' during flag=2 call must be a real vector of length 0
以前见过是在FLAG=3的时候出现,这种情况到没遇到过,麻烦高手给看看。

function = dtcfunc1(t,x,u,flag)

switch flag==3,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
    =mdlInitializeSizes;
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
    sys=mdlOutputs(t,x,u);
   case {1,2,4,9},
    sys=[];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end

function =mdlInitializeSizes

sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs   = 3;
sizes.NumInputs      = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);
x0= [];
str = [];
ts= ;

function sys=mdlOutputs(t,x,u)
u=round(u);
switch u
    case 1;
      sys(1)=0;sys(2)=1;sys(3)=1;
    case 2;
      sys(1)=0;sys(2)=0;sys(3)=1;
    case 3;
      sys(1)=1;sys(2)=0;sys(3)=1;
    case 4;
      sys(1)=1;sys(2)=0;sys(3)=0;
    case 5;
      sys(1)=1;sys(2)=1;sys(3)=0;
    case 6;
      sys(1)=0;sys(2)=1;sys(3)=0;
    case 0;
      sys(1)=0;sys(2)=0;sys(3)=0;
    otherwise
      sys(1)=0;sys(2)=0;sys(3)=0;
end
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;    %Example, set the next hit to be one second later.
sys = t + sampleTime;

function sys=mdlTerminate(t,x,u)
sys = [];

yg0060 发表于 2009-3-26 09:30

把这个改改看怎么样
sizes.DirFeedthrough = 1;
因为你的方程没有状态变量,输出是由输入直接控制的。

nihaojiayafei 发表于 2012-3-21 20:47

单独运行S函数是会报错,但是和Simulink结合仿真就没事了
页: [1]
查看完整版本: 运行S函数出现 flag=2 call must be a real vector of length 0错误