www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/ImportParamCondCompiledDimLengthsExample.m
%% Import Parameter Data with Conditionally Compiled Dimension Length % Suppose your custom code conditionally allocates memory for and % initializes lookup table and breakpoint set data based on a dimension length that you specify % as a |#define| macro. This example shows how to generate code that % imports % this external global data. %% Create Custom Code Files % Save the definition of the breakpoint set data |T1Break| and lookup table % data |T1Data| in your current folder in a file called % |ex_vec_symdim_src.c|. These global variables have either 9 or 11 % elements % depending on the value of the macro |bpLen|. % % <include>ex_vec_symdim_src.c</include> % % Save the declarations of the variables and the definition of the macro in your current folder in a file called % |ex_vec_symdim_decs.h|. % % <include>ex_vec_symdim_decs.h</include> %% Explore and Configure Example Model % Open the example model |rtwdemo_advsc|. open_system('rtwdemo_advsc') %% % Open the Table1 block dialog box. The block refers to % |Simulink.Parameter| objects, |T1Data| and |T1Break|, in the base % workspace. These objects store the lookup table and breakpoint set % data with 11 elements. % % At the command prompt, configure the objects to import the data % definitions from your custom code. T1Data.CoderInfo.StorageClass = 'Custom'; T1Data.CoderInfo.CustomStorageClass = 'ImportFromFile'; T1Data.CoderInfo.CustomAttributes.HeaderFile = 'ex_vec_symdim_decs.h'; T1Break.CoderInfo.StorageClass = 'Custom'; T1Break.CoderInfo.CustomStorageClass = 'ImportFromFile'; T1Break.CoderInfo.CustomAttributes.HeaderFile = 'ex_vec_symdim_decs.h'; %% % At the command prompt, create a |Simulink.Parameter| object to represent % the custom macro |bpLen|. bpLen = Simulink.Parameter(11); bpLen.Min = 9; bpLen.Max = 11; bpLen.DataType = 'int32'; bpLen.CoderInfo.StorageClass = 'Custom'; bpLen.CoderInfo.CustomStorageClass = 'ImportedDefine'; bpLen.CoderInfo.CustomAttributes.HeaderFile = 'ex_vec_symdim_decs.h'; %% % Use |bpLen| to set the dimensions of the lookup table and breakpoint set % data. Configure the model to enable symbolic dimensions by selecting the % configuration parameter *Allow symbolic dimension specification*. T1Data.Dimensions = '[1 bpLen]'; T1Break.Dimensions = '[1 bpLen]'; set_param('rtwdemo_advsc','AllowSymbolicDim','on') %% % Set *Configuration Parameters > Code Generation > Custom Code > % Additional build information > Source files* to |ex_vec_symdim_src.c|. set_param('rtwdemo_advsc','CustomSource','ex_vec_symdim_src.c') %% Generate and Inspect Code % Generate code from the model. rtwbuild('rtwdemo_advsc') %% % The generated code algorithm is in the model |step| function in the % generated file |rtwdemo_advsc.c|. The algorithm passes |T1Break|, |T1Data|, and |bpLen| % as argument values to the function that performs the table lookup. In % this case, |bpLen| controls the upper bound of the binary search that the % function uses. file = fullfile('rtwdemo_advsc_ert_rtw','rtwdemo_advsc.c'); rtwdemodbtype(file,' look1_binlc','bpLen - 1U',1,1) %% % For more information about symbolic dimensions, see <docid:ecoder_ug.bu3c8bh-1>.