| Real-Time Workshop® | ![]() |
| On this page… |
|---|
Creating a MAT-File That Includes a Model's Parameter Structure |
The format and setup of input data for a rapid simulation depends on your requirements.
| If the Input Data Source Is... | Then... |
|---|---|
| The model's global parameter vector (model_P) | Use the rsimgetrtp function to get the vector content and then save it to a MAT-file |
| The model's global parameter vector and you want a mapping between the vector and tunable parameters | Use the rsimgetrtp function with the AddTunableParamInfo option to get the model's global parameter structure and then save it to a MAT-file |
| Provided by a From File block | Create a MAT-file that a From File block can read |
| Provided by an Inport block | Create a MAT-file that adheres to one of the three data file formats that the Inport block can read |
| Provided by a From Workspace block | Create structure variables in the MATLAB® workspace |
The RSim target requires that MAT-files used as input for From File and Inport blocks contain data. The grt target inserts MAT-file data directly into the generated code, which is then compiled and linked as an executable. In contrast, RSim allows you to replace data sets for each successive simulation. A MAT-file containing From File or Inport block data must be present if any From File or Inport blocks exist in your model.
To create a MAT-file that includes a model's global parameter structure (model_P),
Get the structure by calling the function rsimgetrtp.
Save the parameter structure to a MAT-file.
If you want to run simulations over varying data sets, consider converting the parameter structure to a cell array and saving the parameter variations to a single MAT-file.
Get the global parameter structure (model_P) for a model by calling the function rsimgetrtp.
param_struct = rsimgetrtp('model', option)| Argument | Description |
|---|---|
| model | The model for which you are running the rapid simulations. |
| option | The parameter-value pair 'AddTunableParamInfo' 'value', where 'value' can be 'on' or 'off'. If you set the parameter to 'on', the Real-Time Workshop® software extracts tunable parameter information from the specified model and returns it to param_struct. |
The rsimgetrtp function forces an update diagram action for the specified model and returns a structure that contains the following fields:
| Field | Description |
|---|---|
| modelChecksum | A four-element vector that encodes the structure of the model. The Real-Time Workshop software uses the checksum to check whether the structure of the model has changed since the RSim executable was generated. If you delete or add a block, and then generate a new model_P vector, the new checksum no longer matches the original checksum. The RSim executable detects this incompatibility in parameter vectors and exits to avoid returning incorrect simulation results. If the model structure changes, you must regenerate the code for the model. |
| parameters | A structure that contains the model's global parameters. |
If you specify 'AddTunableParamInfo' 'on', the Real-Time Workshop build process creates and then deletes model.rtw from your current working directory and includes the following fields for each parameter in the parameter structure:
| Field | Description |
|---|---|
| dataTypeName | The name of the parameter's data type, for example, double |
| dataTypeID | An internal data type identifier used by the Real-Time Workshop software |
| complex | The value 0 if real and 1 if complex |
To use the AddTunableParamInfo option, inline parameters must be enabled.
The Real-Time Workshop software reports a tunable fixed-point parameter according to its stored value. For example, an sfix(16) parameter value of 1.4 with a scaling of 2^-8 has a value of 358 as an int16.
In the following example, rsimgetrtp returns the parameter structure for the demo model rtwdemo_rsimtf to param_struct.
param_struct = rsimgetrtp('rtwdemo_rsimtf')
param_struct =
modelChecksum: [1.7165e+009 3.0726e+009 2.6061e+009 2.3064e+009]
parameters: [1x1 struct]After you issue a call to rsimgetrtp, save the return value of the function call to a MAT-file. Using a command-line option, you can then specify that MAT-file as input for rapid simulations.
The following example saves the parameter structure returned for rtwdemo_rsimtf to the MAT-file myrsimdemo.mat.
save myrsimdemo.mat param_struct;
For information on using command-line options to specify required files, see Running Rapid Simulations.
If you might have a need to use rapid simulations to test changes to specific parameters, you can do so if you convert the model's parameter structure to a cell array. You can then access a specific parameter by using the @ operator to specify the index for a specific parameter in the file.
To convert the structure to a cell array,
Save the parameters vector of the structure returned by rsimgetrtp to a temporary variable. The following example saves the parameter vector to temporary variable p.
param_struct = rsimgetrtp('rtwdemo_rsimtf');
p = param_struct.parameters;Convert the structure to a cell array.
param_struct.parameters = [];
Assign the saved contents of the temporary variable to the original structure name as an element of the cell array.
param_struct.parameters{1} = p;
param_struct.parameters{1}
ans =
dataTypeName: 'double'
dataTypeId: 0
complex: 0
dtTransIdx: 0
values: [-140 -4900 0 4900]Make a copy of the cell array to preserve the original parameter values.
param_struct.parameters{2} = param_struct.parameters{1};
param_struct.parameters{2}
ans =
dataTypeName: 'double'
dataTypeId: 0
complex: 0
dtTransIdx: 0
values: [-140 -4900 0 4900]For a subsequent data set, increment the array index.
Modify any combination of the parameter values.
param_struct.parameters{2}.values=[-150 -5000 0 4950];Repeat steps 4 and 5 for each parameter data set you want to use as input to a rapid simulation of the model.
Save the cell array representing the parameter structure to a MAT-file.
save rtwdemo_rsimtf.mat param_struct;
Changing Block Parameters for an RSim Simulation explains how to specify each data set when you run the simulations.
You can use a MAT-file as the input data source for a From File block. The format of the data in the MAT-file must match the matrix format expected by that block.
To create such a MAT-file,
Create a matrix in the workspace that consists of two or more rows. The first row must contain monotonically increasing time points. Other rows contain data points that correspond to the time point in that column. The time and data points must be data of type double.
For example:
t=[0:0.1:2*pi]'; Ina1=[2*sin(t) 2*cos(t)]; Ina2=sin(2*t); Ina3=[0.5*sin(3*t) 0.5*cos(3*t)]; var_matrix=[t Ina1 Ina2 Ina3]';
For more information on setting up the input data, see the description of the From File block in the Simulink® documentation.
Save the matrix to a MAT-file.
The following example saves the matrix var_matrix to the MAT-file myrsimdemo.mat.
save myrsimdemo.mat var_matrix;
Using a command-line option, you can then specify that MAT-file as input for rapid simulations.
You can use a MAT-file as the input data source for an Inport block.
The format of the data in the MAT-file must adhere to one of the three column-based formats listed in the following table. The table lists the formats in order from least flexible to most flexible.
| Format | Description |
|---|---|
| Single time/data matrix |
For an example, see Single time/data matrix in step 4 below. For more information, see Importing Data Arrays in the Simulink documentation. |
| Format | Description |
|---|---|
| Signal-and-time structure |
For an example, see Signal-and-time structure in step 4 below. For more information on this format, see Importing Data Structures in the Simulink documentation. |
| Format | Description |
|---|---|
| Per-port structure |
For an example, see Per-port structure in step 4 below. For more information, see Importing Data Structures in the Simulink documentation. |
The supported formats and the following procedure are illustrated in rtwdemo_rsim_i.
To create a MAT-file for an Inport block,
Choose one of the preceding data file formats.
If necessary, update Inport block parameter settings and specifications to match specifications of the data you expect to be supplied by the MAT-file.
By default, the Inport block inherits parameter settings from downstream blocks. In most cases, to import data from an external MAT-file, you need to explicitly set the following parameters to match the source data in the MAT-file.
Main > Interpolate data
Signal Attributes > Port dimensions
Signal Attributes > Data type
Signal Attributes > Signal type
If you choose to use a structure format for workspace variables and the time field is empty, you must clear Interpolate data or modify the field such that it is set to a nonempty value. Interpolation requires time data.
For descriptions of the preceding block parameters, see the description of the Inport block in the Simulink documentation.
Build an RSim executable for the model. The Real-Time Workshop build process creates and calculates a structural checksum for the model and embeds it in the generated executable. The RSim target uses the checksum to verify that data being passed into the model is consistent with what model's executable expects.
Create the MAT-file that is to provide the source data for the rapid simulations. Generally, you can create the MAT-file from a workspace variable. Using the specifications in the format comparison table above, create the workspace variables for your simulations.
An example of each format follows:
t=[0:0.1:2*pi]'; Ina1=[2*sin(t) 2*cos(t)]; Ina2=sin(2*t); Ina3=[0.5*sin(3*t) 0.5*cos(3*t)]; var_matrix=[t Ina1 Ina2 Ina3];
t=[0:0.1:2*pi]'; var_single_struct.time=t; var_single_struct.signals(1).values(:,1)=2*sin(t); var_single_struct.signals(1).values(:,2)=2*cos(t); var_single_struct.signals(2).values=sin(2*t); var_single_struct.signals(3).values(:,1)=0.5*sin(3*t); var_single_struct.signals(3).values(:,2)=0.5*cos(3*t); v=[var_single_struct.signals(1).values... var_single_struct.signals(2).values... var_single_struct.signals(3).values];
t=[0:0.1:2*pi]'; Inb1.time=t; Inb1.signals.values(:,1)=2*sin(t); Inb1.signals.values(:,2)=2*cos(t); t=[0:0.2:2*pi]'; Inb2.time=t; Inb2.signals.values(:,1)=sin(2*t); t=[0:0.1:2*pi]'; Inb3.time=t; Inb3.signals.values(:,1)=0.5*sin(3*t); Inb3.signals.values(:,2)=0.5*cos(3*t);
Save the workspace variables to a MAT-file.
Single time/data matrix
The following example saves the workspace variable var_matrix to the MAT-file rsim_i_matrix.mat.
save rsim_i_matrix.mat var_matrix;
Signal-and-time structure
The following example saves the workspace structure variable var_single_struct to the MAT-file rsim_i_single_struct.mat.
save rsim_i_single_struct.mat var_single_struct;
Per-port structure
To ensure correct ordering of data when saving per-port structure variables to a single MAT-file, use the save command's -append option. Be sure to append the data in the order that the model expects it.
The following example saves the workspace variables Inb1, Inb2, and Inb3 to MAT-file rsim_i_multi_struct.mat.
save rsim_i_multi_struct.mat Inb1; save rsim_i_multi_struct.mat Inb2 -append; save rsim_i_multi_struct.mat Inb3 -append;
The save command does not preserve the order in which you specify your workspace variables in the command line when saving data to a MAT-file. For example, if you specify the variables v1, v2, and v3, in that order, the order of the variables in the MAT-file could be v2 v1 v3.
Using a command-line option, you can then specify the MAT-files as input for rapid simulations.
![]() | Configuring and Building a Model for Rapid Simulation | Programming Scripts for Batch and Monte Carlo Simulations | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |