www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/ImportParameterValueFromCustomCodeExample.m

    %% Reuse Parameter Data from Custom Code in the Generated Code
% This example shows how to generate code that imports a parameter value
% from your external, custom code.
%% Create Custom Code Files
% Suppose your custom code defines a vector parameter |myGains| with three elements.  Save the
% definition in your current folder in a file called
% |ex_vector_import_src.c|.
%
% <include>ex_vector_import_src.c</include>
%
% Save the declaration in your current folder in a file called
% |ex_vector_import_decs.h|.
%
% <include>ex_vector_import_decs.h</include>
%
% Save the custom data type definition |my_int8| in your current folder in
% a file called |ex_vector_import_cust_types.h|.
%
% <include>ex_vector_import_cust_types.h</include>
%% Import Parameter Value for Simulation
% In your current folder, right-click the file |ex_vector_import_src.c| and
% select *Import Data*.
%
% In the Import dialog box, set the name of the generated MATLAB variable
% to |tempVar|.
%
% Select only the parameter values (|2|, |4|, and |6|) to import.
%
% <<../import_dialog_box.png>>
%
% Import the data by clicking the green check mark. The MATLAB variable |tempVar|
% appears in the base workspace.
%
% Alternatively, use the command prompt to manually create |tempVar|.
tempVar = [2;4;6];
%% Create and Configure Model
% Create the model |ex_vector_import|.
open_system('ex_vector_import')
%%
% In the Gain block dialog box, on the *Parameter Attributes* tab, set
% *Parameter data type* to |Inherit: Inherit from 'Gain'|.
%
% On the *Main* tab, set *Gain* to |myGains|. Click *Apply*.
%
% Right-click |myGains| and select *Create Variable*.
%
% In the Create New Data dialog box, set *Value* to |Simulink.Parameter|
% and click *Create*.
%
% In the myGains dialog box, set these property values and click *OK*:
%
% * *Data type* to |my_int8|
% * *Storage class* to |ImportFromFile|
% * *HeaderFile* to |ex_vector_import_decs.h|
%
% Alternatively, use these commands at the command prompt to create the object and set the
% property values:
myGains = Simulink.Parameter;
set_param('ex_vector_import/Gain','Gain','myGains',...
    'ParamDataTypeStr','Inherit: Inherit from ''Gain''')
myGains.DataType = 'my_int8';
myGains.CoderInfo.StorageClass = 'Custom';
myGains.CoderInfo.CustomStorageClass = 'ImportFromFile';
myGains.CoderInfo.CustomAttributes.HeaderFile = 'ex_vector_import_decs.h';
%%
% At the command prompt, set the |Value| property by using the value of
% |tempVar|.
myGains.Value = tempVar;
%%
% At the command prompt, create a |Simulink.AliasType| object to represent
% your custom data type |my_int8|. Set the |DataScope| and |HeaderFile|
% properties to import the type definition from your custom code.
my_int8 = Simulink.AliasType('int8');
my_int8.DataScope = 'Imported';
my_int8.HeaderFile = 'ex_vector_import_cust_types.h';
%%
% Set *Configuration Parameters > Code Generation > Custom Code >
% Additional build information > Source files* to |ex_vector_import_src.c|.
set_param('ex_vector_import','CustomSource','ex_vector_import_src.c')
%% Generate and Inspect Code
% Generate code from the model.
rtwbuild('ex_vector_import')
%%
% The generated file |ex_vector_import.h| includes the custom header files
% |ex_vector_import_decs.h| and |ex_vector_import_cust_types.h|, which
% contain the parameter variable declaration (|myGains|) and custom type definition (|my_int8|).
file = fullfile('ex_vector_import_ert_rtw','ex_vector_import.h');
rtwdemodbtype(file,'/* Includes for objects with custom storage classes. */',...
    '#include "ex_vector_import_cust_types.h"',1,1)
%%
% The generated code algorithm in the model |step| function in the
% generated file |ex_vector_import.c| uses |myGains| for calculations.
file = fullfile('ex_vector_import_ert_rtw','ex_vector_import.c');
rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)
%%
% The generated code does
% not define (allocate memory for) or initialize the global variable
% |myGains| because the data scope of the corresponding parameter object is
% imported.
%
% When you simulate the model in Simulink, the model uses the value stored in the
% |Value| property of the parameter object. However, if you use external
% mode simulation, the external executable begins the simulation by using
% the value from your code. See <docid:ecoder_ug.bvbdgwg>.