No BSD License  

Highlights from
Examples of incorporating m-code into Simulink models

Examples of incorporating m-code into Simulink models

by

 

23 Sep 2004 (Updated )

Example Simulink models to support a MATLAB Digest article from September 2004.

aero_plot_L2_msfcn(block)
function aero_plot_L2_msfcn(block)
%MSFUNTMPL A template for an M-file S-function
%   The M-file S-function is written as a MATLAB function with the
%   same name as the S-function. 
%
%   It should be noted that the M-file S-function is very similar
%   to Level-2 C-Mex S-functions. You should be able to get more 
%   information for each of the block methods by referring to the
%   documentation for C-Mex S-functions.
%  
%   Copyright 2003-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.5 $  
  setup(block);
  
%endfunction

%% Function: setup ===================================================
%% Abstract:
%%   Set up the S-function block's basic characteristics such as:
%%   - Input ports
%%   - Output ports
%%   - Dialog parameters
%%   - Options
%% 
%%   C-Mex S-function counterpart: mdlInitializeSizes
%%
function setup(block)
  
  %% Register number of dialog parameters   
  block.NumDialogPrms = 0;
  
  %% Register number of input and output ports
  block.NumInputPorts  = 3;
  block.NumOutputPorts = 0;

  % Setup port properties to be inherited or dynamic
  %block.SetPreCompInpPortInfoToDynamic;
  %block.SetPreCompOutPortInfoToDynamic;
  
  block.InputPort(1).Complexity    = 'Real'; 
  block.InputPort(1).Dimensions    = 1;
  block.InputPort(2).Complexity    = 'Real'; 
  block.InputPort(2).Dimensions    = 2;
  block.InputPort(3).Complexity    = 'Real'; 
  block.InputPort(3).Dimensions    = 2;
    
  %% Set block sample time 
  %block.SampleTimes = [block.DialogPrm(1).Data 0];
  
  %block.RegBlockMethod('PostPropagationSetup',    @DoPostPropSetup);
  %block.RegBlockMethod('InitializeConditions', @InitializeConditions);
  %block.RegBlockMethod('Outputs',@Output);  
  block.RegBlockMethod('Update', @Update);
  
%% Run accelerator on TLC
  block.SetAccelRunOnTLC(false);
  
function DoPostPropSetup(block)
  
  %% Setup Dwork  
                         
  block.NumDworks = 0;

function InitializeConditions(block)

%endfunction

function Update(block)

t = block.InputPort(1).Data;
x = block.InputPort(2).Data;
y = block.InputPort(3).Data;

doPlot(t,x,y);

function doPlot(t,x,y)
if t == 0
    hold('off'); clf;
    axis([-10000 30000 -10000 80000]); hold('on')
    plot([-10000 30000],[0 0])
    plot([0 0],[-10000 80000])
    title('\bf{Kalman Filter Using level 2 m-file S-function}')
    text(-40000,-25000,'\rm\it{Position Measurements (Blue) & Position Estimates (Red)}')
    grid('on')
   
end
plot(x(1),y(1),'Marker','+','MarkerSize',3)
plot(x(2),y(2),'Marker','*','Color',[1 0 0],'MarkerSize',3)

Contact us