www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/UseGetSetWithVectorDataExample.m

    %% Use |GetSet| with Vector Data
% This example shows how to apply the custom storage class |GetSet| to
% signals and parameters that are vectors.
%
% 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 |vectors| is a substructure that uses the structure type
% |VectorData|. The structure type |VectorData| defines three vector
% fields: 
% |inVector|, |vectorParam|, and |outVector|. The vectors each have five
% elements.
rtwdemodbtype('ComponentDataHdr.h','/* VectorData */','} VectorData;',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 |vectors|.
rtwdemodbtype('getsetSrc.c','/* Field "vectors" */','/* End of "vectors" */',1,1)
%%
% The file also defines functions that read from and write to the fields of
% the substructure |vectors|. The functions simplify data access by dereferencing the leaf fields of
% the global structure variable |ex_getset_data|. To access the vector
% data, all of the functions accept an integer index argument. The |get| function returns
% the vector value at the input index. The |set| function assigns the
% input |value| to the input index.
rtwdemodbtype('getsetSrc.c',...
    '/* Vector get() and set() functions */','/* End of vector functions */',1,1)
%%
% View the example legacy header file <matlab:edit('getsetHdrVector.h') |getsetHdrVector.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_vector') |rtwdemo_getset_vector|>. The model creates the data objects |inVector|,
% |outVector|, and |vectorParam| in the base workspace. The objects
% correspond to the signals and parameter in the model.
open_system('rtwdemo_getset_vector')
%%
% In the base workspace, double-click the object |inVector| to view its properties. The object uses the custom storage class |GetSet|. The property
% |HeaderFile| is specified as |getsetHdrVector.h|. This legacy header file
% contains the |get| and |set| function prototypes.
%
% In the model Configuration Parameters dialog box, on the *Code Generation
% > Custom Code* pane, the example legacy source file |getsetSrc.c| is
% identified 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_vector');
%%
% In the code generation report, view the file |rtwdemo_getset_vector.c|. The
% model |step| function uses the legacy |get| and |set| functions to execute the
% algorithm.
rtwdemodbtype(fullfile('rtwdemo_getset_vector_ert_rtw','rtwdemo_getset_vector.c'),...
    '/* Model step function */','}',1,1)
%%
% When you use the custom storage class |GetSet| with vector data, the
% |get| and |set| functions that you provide must accept an index input.
% The |get| function must return a single element of the vector. The
% |set| function must write to a single element of the vector.