www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/OptimizeModelRefInitCodeExample.m
%% Optimize Initialization Code for a Referenced Model % This example shows how to optimize generated code by suppressing % initialization code for a referenced model. The referenced model must % contain blocks that have initial conditions set to zero and are not in a % system that can reset its states, such as an enabled subsystem. % % This optimization: % % * Improves execution speed. % * Reduces ROM consumption. % % Copyright 2014 The MathWorks, Inc. %% Example Model % A referenced model, <matlab:rtwdemo_optimizeModelRefInitCode_bot rtwdemo_optimizeModelRefInitCode_bot>, is in % <matlab:rtwdemo_optimizeModelRefInitCode_top rtwdemo_optimizeModelRefInitCode_top>. The referenced model % contains two blocks with states, a Unit Delay block and a Discrete Time % Integrator block. The initial conditions for these blocks are set to zero % (default value). modelBot = 'rtwdemo_optimizeModelRefInitCode_bot'; load_system(modelBot); set_param(modelBot, 'OptimizeModelRefInitCode', 'off'); modelBotMod = 'rtwdemo_optimizeModelRefInitCode_bot_mod'; save_system(modelBot,modelBotMod); open_system(modelBot); modelTop = 'rtwdemo_optimizeModelRefInitCode_top'; open_system(modelTop); set_param([modelTop '/Model'], 'ModelName', modelBotMod); set_param(modelTop, 'OptimizeModelRefInitCode', 'off'); %% Generate Code % In your system temporary folder, create a temporary folder for the % build and inspection process. currentDir = pwd; [~,cgDir] = rtwdemodir(); %% % Build the model. rtwbuild(modelTop) %% % View the generated code without the optimization. Here is a portion of % |rtwdemo_optimizeModelRefInitCode_top.c|. cfile = fullfile(cgDir,'rtwdemo_optimizeModelRefInitCode_top_ert_rtw',... 'rtwdemo_optimizeModelRefInitCode_top.c'); rtwdemodbtype(cfile,'/* Model initialize', '/* Model terminate', 1, 0); %% Enable Optimization % # Open the Configuration Parameters dialog box. % # On the *Optimization* pane, select *Optimize initialization code for model reference.* % % Alternatively, you can use the command-line API to enable the optimization: set_param(modelBotMod, 'OptimizeModelRefInitCode', 'on'); save_system(modelBotMod); set_param(modelTop, 'OptimizeModelRefInitCode', 'on'); %% Generate Code with Optimization % In the optimized code, there is no initialization code for the referenced model. % % Build the model. rtwbuild(modelTop) %% % View the optimized code in |rtwdemo_optimizeModelRefInitCode_top.c|. cfile = fullfile(cgDir,'rtwdemo_optimizeModelRefInitCode_top_ert_rtw',... 'rtwdemo_optimizeModelRefInitCode_top.c'); rtwdemodbtype(cfile,'/* Model initialize', '/* Model terminate', 1, 0); %% % Close the model and the code generation report. bdclose(modelTop) rtwdemoclean; cd(currentDir)