建立一个S-funcion,这里有一个将数据输出到GUI的代码 请复制使用一下

function [sys,x0,str,ts] = gui_plot_sfun(t,x,u,flag)

%

switch flag,

%%%%%%%%%%%%%%%%%%

% Initialization %

%%%%%%%%%%%%%%%%%%

case 0,

[sys,x0,str,ts]=mdlInitializeSizes;

%%%%%%%%%%%%%%%

% Derivatives %

%%%%%%%%%%%%%%%

case 1,

sys=mdlDerivatives(t,x,u);

%%%%%%%%%%

% Update %

%%%%%%%%%%

case 2,

sys=mdlUpdate(t,x,u);

%%%%%%%%%%%

% Outputs %

%%%%%%%%%%%

case 3,

sys=mdlOutputs(t,x,u);

%%%%%%%%%%%%%%%%%%%%%%%

% 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

%

% 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     = 0;

sizes.NumInputs      = 1;

sizes.DirFeedthrough = 1;   % u is used in Output function so it is 1 here.

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  = [0 0];

% init the ui controls

global t f vec

f = figure('Position', [280, 280,330, 330], 'MenuBar', 'none');

t = uicontrol(f,'Style','text','Position',[180,200,80,30]);

vec = [];

% end mdlInitializeSizes

%

%=============================================================================

% mdlDerivatives

% Return the derivatives for the continuous states.

%=============================================================================

%

function sys=mdlDerivatives(t,x,u)

sys = [];

% end mdlDerivatives

%

%=============================================================================

% mdlUpdate

% Handle discrete state updates, sample time hits, and major time step

% requirements.

%=============================================================================

%

function sys=mdlUpdate(t,x,u)

sys=[];

% end mdlUpdate

%

%=============================================================================

% mdlOutputs

% Return the block outputs.

%=============================================================================

%

function sys=mdlOutputs(t,x,u)

pause(0.5);

global t vec f

vec = [vec, u];

set(t, 'string', num2str(u));

plot(vec);

sys = [];

% end mdlOutputs

%

%=============================================================================

% 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)

global f

close(f);

sys = [];

% end mdlTerminate

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐