ljh617 发表于 2009-3-21 22:08

用s函数做的simulink模块--不能保存

用S函数做一个密钥产生器,能够正常进行运行,结果正确。但是保存模块式出现错误提示(详见附件 错误.jpg)
参数:len=21
:victory: :victory: :@) :@) 问题已解决参见:http://www.programfan.com/club/showtxt.asp?id=171598
function = sfun_Key_Generator(t,x,u,flag,len)

%
% The following outlines the general structure of an S-function.
%
%registersone=;%寄存器初始化
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
    =mdlInitializeSizes(len);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
    sys=mdlDerivatives();
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
    sys=mdlUpdate(x,len);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
    sys=mdlOutputs(x);
%%%%%%%%%%%%%%%%%%%%%%%
% GetTimeOfNextVarHit %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
    sys=mdlGetTimeOfNextVarHit(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
    sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%
% 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 =mdlInitializeSizes(len)
%
% 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= 22;
sizes.NumOutputs   = 1;
sizes.NumInputs      = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);
%
% initialize the initial conditions
%
x0= ;
%
% str is always an empty matrix
%
str = [];
%
% initialize the array of sample times
%
ts= ;
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
%ms=registersone(1)+1+registersone(20);
%   ms=mod(ms,2);                     
sys = [];
% end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(x,len)
msi=x(1)+1+x(20);
%assignin('base','msi',msi);
   x(22)=mod(msi,2);
   x(2:len)=x(1:len-1);
   x(1)=x(22);
sys(1:21) = x(1:21);
sys(22)=x(22);
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(x)
%assignin('base','x',x);
sys = x(22);
%assignin('base','sys',sys);
%sys
% end mdlOutputs
% function ft=my_registersone(registersone)
%      msi=registersone(1)+1+registersone(20);
% %assignin('base','msi',msi);
%      x(22)=mod(msi,2);
%      registersone(2:len)=registersone(1:len-1);
%      registersone(1)=x(22);
%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block.Note that the result is
% absolute time.Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;    %Example, set the next hit to be one second later.
sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate

[ 本帖最后由 ljh617 于 2009-3-21 22:54 编辑 ]
页: [1]
查看完整版本: 用s函数做的simulink模块--不能保存