www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/IntExtCodeIntoAModelForCodeGenerationUsingStateflowExample.m

    %% Insert External C and C++ Code Into Stateflow Charts for Code Generation
% This example shows how to use Stateflow(R) to integrate external code
% into a model. 
%
%% Open Model

model='rtwdemo_sfcustom';
open_system(model);

%% Integrate Code
% 
% 1. The example includes the custom header file |my_header.c| and the
% custom source file |my_function.c|.
%%
%Open files my_header.h and my_function.c
eval('edit my_header.h')
eval('edit my_function.c')

%%
% 2. On the Configuration Parameters dialog box *Simulation Target*
% pane, enter the custom source file and header file. Also enter additional 
% include directories and source files. 
%
% In this example, the custom header file |my_header.c| and source file |my_function.c|
% are entered on the *Simulation Target* pane.
%%
%Open Configuration Parameters dialog box                              
slCfgPrmDlg(model,'Open');                  
slCfgPrmDlg(bdroot,'TurnToPage','Simulation Target');

%%
% 3. If you generate code with Simulink Coder(R), on the Configuration Parameters 
% dialog box *Code Generation > Custom Code* pane, enter the same custom source 
% file and header file. Also enter the same additional include directories 
% and source files.
%
% In this example, the custom header file |my_header.c| and source file |my_function.c|
% are entered on the *Code Generation > Custom Code* pane.
%%
%Open Configuration Parameters dialog box                             
slCfgPrmDlg(model,'Open');                  
slCfgPrmDlg(bdroot,'TurnToPage','Code Generation/Custom Code');

%% Generate Code
%
rtwbuild('rtwdemo_sfcustom')

%% Call C Code from Stateflow
% To call custom C code functions from Stateflow, use the same syntax as graphical
% function calls:  |result = my_custom_function(in_args);|
%%
% To call variables of structure type, use the dot notation: |result = my_var.my_field;|

%% See Also
% * <matlab:helpview(fullfile(docroot,'stateflow','stateflow.map'),'TARGET_OPTIONS_DIALOG'); Include Custom C Code in Simulation Targets for Library Models>
% * <matlab:helpview(fullfile(docroot,'stateflow','stateflow.map'),'including_custom_cpp_code'); Integrate Custom C++ Code for Simulation>

%% Close Model

rtwdemoclean;
close_system('rtwdemo_sfcustom',0);