www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/CreateTunableParameterStructureInTheGeneratedCodeExample.m
%% Structures of Parameters % Create a structure in the generated code. The structure stores parameter data. %% C Construct % <include>CreateTunableParamStruct.c</include> %% Procedure % At the command prompt, create a structure named |myStruct| with two fields. myStruct.G1 = 2; myStruct.G2 = -2; %% % Store the structure in a |Simulink.Parameter| object. myStruct = Simulink.Parameter(myStruct); %% % Apply the storage class |ExportedGlobal| so that the structure appears in the % generated code as a global variable. myStruct.CoderInfo.StorageClass = 'ExportedGlobal'; %% % Open the example model |rtwdemo_paraminline|. rtwdemo_paraminline %% % In the G1 block dialog box, set *Gain* to |myStruct.G1|. set_param('rtwdemo_paraminline/G1','Gain','myStruct.G1') %% % In the G2 block dialog box, set *Gain* to |myStruct.G2|. set_param('rtwdemo_paraminline/G2','Gain','myStruct.G2') %% Results % Generate code from the model. rtwbuild('rtwdemo_paraminline') %% % The generated header file |rtwdemo_paraminline_types.h| defines a structure type % with a randomized name. file = fullfile('rtwdemo_paraminline_grt_rtw',... 'rtwdemo_paraminline_types.h'); rtwdemodbtype(file,'typedef struct {','} struct_6h72eH5WFuEIyQr5YrdGuB;',... 1,1) %% % The source file |rtwdemo_paraminline.c| defines and initializes the % structure variable |myStruct|. file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c'); rtwdemodbtype(file,'struct_6h72eH5WFuEIyQr5YrdGuB myStruct',... '/* Variable: myStruct',1,1) %% Specify Name of Structure Type % Optionally, specify a name to use for the structure type definition % (|struct|). % % Create a |Simulink.Bus| object that represents the structure type. Simulink.Bus.createObject(myStruct.Value); %% % The % default name of the object is |slBus1|. Change the name by copying the % object into a new MATLAB variable. myStructType = slBus1.copy; %% % Use the bus object as the data type of the parameter object. myStruct.DataType = 'Bus: myStructType'; %% % Generate code from the model. rtwbuild('rtwdemo_paraminline') %% % The code generates the definition of the structure type |myStructType| % and uses this type to define the global variable |myStruct|. rtwdemodbtype(file,'myStructType myStruct = {','/* Variable: myStruct',... 1,1)