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

    %% Represent Block Parameter as Tunable Global Variable
% This example shows how to create tunable parameter data 
% by representing a block parameter as a global variable in the generated
% code.
%% Configure Block Parameter by Using Parameter Object
% Open the example model <matlab:rtwdemo_paraminline |rtwdemo_paraminline|>.
rtwdemo_paraminline
%%
% In the G1 block dialog box, change *Gain* from |2| to |myGainParam|. Click *Apply*.
%
% Right-click |myGainParam| and select *Create Variable*.
%
% In the Create New Data block dialog box, set *Value* to
% |Simulink.Parameter(2)|. Click *Create*. A |Simulink.Parameter| object
% |myGainParam| stores the parameter value, |2|, in the base workspace.
%
% In the myGainParam dialog box, set *Storage class* to |ExportedGlobal| and click *OK*. This
% storage class causes the parameter object to appear in the generated code
% as a tunable global variable.
%
% Alternatively, to create the
% parameter object and configure the model, use these commands at the command prompt:
set_param('rtwdemo_paraminline/G1','Gain','myGainParam')
myGainParam = Simulink.Parameter(2);
myGainParam.CoderInfo.StorageClass = 'ExportedGlobal';
%% Generate and Inspect Code
% Generate code from the model.
rtwbuild('rtwdemo_paraminline')
%%
% The generated file |rtwdemo_paraminline.h| contains an |extern|
% declaration of the global variable |myGainParam|. You can include (|#include|)
% this header file so that your code can read and write the value of the
% variable
% during execution.
file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.h');
rtwdemodbtype(file,'extern real_T myGainParam;','extern real_T myGainParam;',1,1)
%%
% The file |rtwdemo_paraminline.c| allocates memory for and initializes
% |myGainParam|.
file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(file,'/* Exported block parameters */','real_T myGainParam = 2.0;',1,1)
%%
% The generated code algorithm in the model |step| function uses |myGainParam|
% for calculations.
rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)