www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/EnumerationCModelingPatternExample.m
%% Enumeration % To generate an enumerated data type, define an enumeration class in a % MATLAB file. Then, use the enumeration class as the data type of signals, % block parameters, and states in a model. %% C Construct % <include>c_construct_enum.txt</include> %% Procedure % In your current folder, create the MATLAB file |myEnumType.m|. The % file defines an enumeration class |myEnumType|. % % <include>myEnumType.m</include> % % Create the model |ex_pattern_enum| by using an Enumerated Constant block % and a Multiport Switch block. open_system('ex_pattern_enum') %% % In the base workspace, create a |Simulink.Parameter| object |myChoice|. % Use the enumeration member |Choice1| to set the value of the parameter % object. myChoice = Simulink.Parameter(myEnumType.Choice1); %% % Set the storage class of the parameter object to |ExportedGlobal| so that the % object appears in the generated code as a global variable. myChoice.CoderInfo.StorageClass = 'ExportedGlobal'; %% % In the Enumerated Constant block dialog box, set: % % * *Output data type* to |Enum: myEnumType|. % * *Value* to |myChoice|. % % In the Multiport Switch block dialog box, set: % % * *Data port order* to |Specify indices|. % * *Data port indices* to |enumeration('myEnumType')|. This expression % returns all of the enumeration members of |myEnumType|. % % Generate code from the model. rtwbuild('ex_pattern_enum'); %% Results % View the generated header file |myEnumHdr.h|. The file defines the enumerated % data type. file = fullfile('ex_pattern_enum_ert_rtw','myEnumHdr.h'); rtwdemodbtype(file,'typedef enum {','} myEnumType;',1,1) %% % View the source file |ex_pattern_enum.c|. The file % defines the variable |myChoice|. The algorithm in the |step| function uses |myChoice| to % route one of the input signals to the output signal. file = fullfile('ex_pattern_enum_ert_rtw','ex_pattern_enum.c'); rtwdemodbtype(file,'myEnumType myChoice = Choice1;','/* Variable: myChoice',1,1) rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)