www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/AccessLegacyDataUsingGetAndSetFunctionsExample.m
%% Access Legacy Data Using |Get| and |Set| Functions % This example shows how to generate code that interfaces with legacy code % by using specialized |get| and |set| functions to access data. % % View the example legacy header file <matlab:edit('ComponentDataHdr.h') |ComponentDataHdr.h|>. The file % defines a large structure type |ComponentData|. rtwdemodbtype('ComponentDataHdr.h','/* ComponentData */','} ComponentData;',1,1) %% % The field |scalars| is a substructure that uses the structure type % |ScalarData|. The structure type |ScalarData| defines three scalar % fields: % |inSig|, |scalarParam|, and |outSig|. rtwdemodbtype('ComponentDataHdr.h','/* ScalarData */','} ScalarData;',1,1) %% % View the example legacy source file <matlab:edit('getsetSrc.c') |getsetSrc.c|>. The file defines and % initializes a global variable |ex_getset_data| that uses the structure % type |ComponentData|. The initialization includes values for the % substructure |scalars|. rtwdemodbtype('getsetSrc.c','/* Field "scalars" */','/* End of "scalars" */',1,1) %% % The file also defines functions that read from and write to the fields of % the substructure |scalars|. The functions simplify data access by dereferencing the leaf fields of % the global structure variable |ex_getset_data|. rtwdemodbtype('getsetSrc.c',... '/* Scalar get() and set() functions */','/* End of scalar functions */',1,1) %% % View the example legacy header file <matlab:edit('getsetHdrScalar.h') |getsetHdrScalar.h|>. The file % contains the |extern| prototypes for the |get| and |set| functions % defined in |getsetSrc.c|. % % Open the example model % <matlab:open_system('rtwdemo_getset_scalar') |rtwdemo_getset_scalar|>. The model creates the data objects |inSig|, |outSig|, and % |scalarParam| in the base workspace. The objects correspond to the % signals and parameter in the model. open_system('rtwdemo_getset_scalar') %% % In the base workspace, double-click the object |inSig| to view its % properties. The object uses the custom storage class |GetSet|. The |GetFunction| and % |SetFunction| properties are set to the defaults, |get_$N| and |set_$N|. The generated code uses the function names that you specify in |GetFunction| and |SetFunction| to read from % and write to the data. The code replaces the token |$N| with the name of % the data object. For example, for the data object |inSig|, the generated % code uses calls to the legacy functions |get_inSig| and |set_inSig|. % % For the data object |inSig|, the |HeaderFile| property is set to |getsetHdrScalar.h|. % This legacy header file contains the |get| and |set| function prototypes. The data objects |outSig| and |scalarParam| also use the custom % storage class |GetSet| and the header file |getsetHdrScalar.h|. % % In the model Configuration Parameters dialog box, on the *Code Generation % > Custom Code* pane, under *Include list of additional*, select *Source % files*. The *Source files* box identifies the source file |getsetSrc.c| % for inclusion during the build process. This legacy source file contains % the |get| and |set| function definitions and the definition of the global structure % variable |ex_getset_data|. % % Generate code with the example model. rtwbuild('rtwdemo_getset_scalar'); %% % In the code generation report, view % the file |rtwdemo_getset_scalar.c|. The model |step| function uses the legacy |get| and % |set| functions to execute the algorithm. The generated code accesses the % legacy signal and parameter data by calling the custom, handwritten |get| and |set| functions. rtwdemodbtype(fullfile('rtwdemo_getset_scalar_ert_rtw','rtwdemo_getset_scalar.c'),... '/* Model step function */','}',1,1) %% % You can generate code that calls your custom |get| and |set| functions as long as % the functions that you write accept and return the expected values. % For scalar data, the functions must have these % characteristics: % % * The |get| function must return a single scalar numeric value of the % appropriate data type, and must not accept any arguments (|void|). % * The |set| function must not return anything (|void|), and must accept a % single scalar numeric value of the appropriate data type.