%% Use |GetSet| with Matrix Data
% This example shows how to apply the storage class |GetSet| to
% signals and parameters that are matrices.
%
% 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 |matrices| is a substructure that uses the structure type
% |MatricesData|. The structure type |MatricesData| defines three fields:  
% |matrixInput|, |matrixParam|, and |matrixOutput|. The fields store matrix
% data as serial arrays. In this case, the input and parameter fields each
% have 15
% elements. The output field has nine elements.
rtwdemodbtype('ComponentDataHdr.h'...
    ,'/* MatricesData */','} MatricesData;',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 |matrices|.
rtwdemodbtype('getsetSrc.c',...
    '/* Field "matrices" */','/* End of "matrices" */',1,1)
%%
% The input matrix has five rows and three columns. The matrix parameter
% has three rows and five columns. The matrix output has three rows and
% three columns. The file defines macros that indicate these 
% dimensions.
rtwdemodbtype('getsetSrc.c',...
    '/* Matrix dimensions */','/* End of matrix dimensions */',1,1)
%%
% The file also defines functions that read from and write to the fields of
% the substructure |matrices|.
rtwdemodbtype('getsetSrc.c',...
    '/* Matrix get() and set() functions */','/* End of matrix functions */',1,1)
%%
% The code that you generate from a model represents
% matrices as serial arrays. Therefore, each of the |get| and |set|
% functions accept a single scalar index argument.
%
% The generated code uses column-major format to store and to access matrix
% data. However, many C applications use row-major indexing. To integrate
% the generated code with the example legacy code, which stores the
% matrices |matrixInput| and |matrixParam| using row-major format, the
% custom |get| functions use the column-major index input to calculate an
% equivalent row-major index. The generated code algorithm, which
% interprets matrix data using column-major format by default, performs the
% correct matrix math because the |get| functions effectively convert the
% legacy matrices to column-major format. The |set|
% function for the output, |matrixOutput|, also calculates a row-major
% index so the code writes the algorithm output to |matrixOutput|
% using row-major format. Alternatively, to
% integrate the column-major generated code with your row-major legacy
% code, you can manually convert the legacy code to column-major format by
% transposing your matrix data and algorithms.
%
% View the example legacy header file <matlab:edit('getsetHdrMatrix.h') |getsetHdrMatrix.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_matrix') |rtwdemo_getset_matrix|>. The model creates the data objects |matrixInput|,
% |matrixParam|, and |matrixOutput| in the base workspace. The objects
% correspond to the signals and parameter in the model.
load_system('rtwdemo_getset_matrix')
set_param('rtwdemo_getset_matrix','SimulationCommand','Update')
open_system('rtwdemo_getset_matrix')
%%
% In the base workspace, double-click the object |matrixInput| to view its properties. 
% The object uses the storage class |GetSet|. The property
% |HeaderFile| is specified as |getsetHdrMatrix.h|. This legacy header file
% contains the |get| and |set| function prototypes.
%
% In the 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_matrix');
%%
% In the code generation report, view the file |rtwdemo_getset_matrix.c|. The
% model |step| function uses the legacy |get| and |set| functions to execute the
% algorithm.
rtwdemodbtype(fullfile('rtwdemo_getset_matrix_ert_rtw',...
    'rtwdemo_getset_matrix.c'),'/* Model step function */','}',1,1)

%% 
% Copyright 2019 The MathWorks, Inc.