声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1662|回复: 0

[基本模块类] 用s函数做的simulink模块--不能保存

[复制链接]
发表于 2009-3-21 22:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

%
% The following outlines the general structure of an S-function.
%
%registersone=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0];%寄存器初始化
switch flag,
  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,str,ts]=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 [sys,x0,str,ts]=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  = [1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0];
%
% str is always an empty matrix
%
str = [];
%
% initialize the array of sample times
%
ts  = [1 0];
% 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 编辑 ]

保存时的错误提示

保存时的错误提示

仿真连接

仿真连接

仿真结果

仿真结果
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-13 12:13 , Processed in 0.061603 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表