Set Up Rapid Simulation Input Data
About Rapid Simulation Data Setup
The format and setup of input data for a rapid simulation depends on your requirements.
If the Input Data Source Is... | Then... |
---|---|
The model global parameter vector
( ) | Use the rsimgetrtp function to get the vector
content and then save it to a MAT-file. |
The model global parameter vector and you want a mapping between the vector and tunable parameters | Call the rsimgetrtp function to get the 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 a From File block or Inport
block exists in your model.
Create a MAT-File That Includes a Model Parameter Structure
To create a MAT-file that includes a model 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 Parameter Structure for a Model
Get the global parameter structure
(
) for a model by calling
the function model
_Prsimgetrtp
.
param_struct = rsimgetrtp('model')
Argument | Description |
---|---|
| The model for which you are running the rapid simulations. |
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 code generator 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 does not match the original checksum anymore. 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. |
The parameter structure contains this information.
Field | Description |
---|---|
dataTypeName | The name of the parameter data type, for example,
double |
dataTypeID | Internal data type identifier used by the code generator |
complex | The value 0 if real; 1 if complex |
dtTransIdx | Internal data index used by the code generator |
values | A vector of the parameter values associated with this structure |
map | This field contains the mapping information that correlates the
'values' to the tunable parameters of the model. This mapping
information, in conjunction with
rsimsetrtpparam , is useful for creating
subsequent rtP structures without compiling the
block diagram. |
The code generator 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 this example, rsimgetrtp
returns the parameter structure
for model rapidsim
to param_struct
.
param_struct = rsimgetrtp('rapidsim')
param_struct = modelChecksum: [1.7165e+009 3.0726e+009 2.6061e+009 2.3064e+009] parameters: [1x1 struct]
Save the Parameter Structure to a MAT-File
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.
This example saves the parameter structure returned for a model to the MAT-file
rapidsim.mat
.
save rapidsim.mat param_struct;
For information on using command-line options to specify required files, see Run Rapid Simulations.
Convert the Parameter Structure for Running Simulations on Varying Data Sets
To use rapid simulations to test changes to specific parameters, you can convert the model parameter structure to a cell array. You can then access a specific parameter set by using the @ operator to specify the index for a specific parameter set in the file.
To convert the structure to a cell array:
Use the function
rsimgetrtp
to get a structure containing parameter information for the model. Store the structure in a variableparam_struct
.param_struct = rsimgetrtp('rapidsim');
The
parameters
field of the structure is a substructure that contains parameter information. Thevalues
field of theparameters
substructure contains the numeric values of the parameters that you can tune during execution of the simulation code.Use the function
rsimsetrtpparam
to expand the structure so that it contains more parameter sets. In this case, create two more parameter sets (for a total of three sets).param_struct = rsimsetrtpparam(param_struct,3);
The function converts the
parameters
field to a cell array with three elements. Each element contains information for a single parameter set. By default, the function creates the second and third elements of the cell array by copying the first element. Therefore, the parameter sets use the same parameter values.Specify new values for the parameters in the second and third parameter sets.
param_struct.parameters{2}.values = [-150 -5000 0 4950]; param_struct.parameters{3}.values = [-170 -5500 0 5100];
Save the structure containing the parameter set information to a MAT-file.
save rapidsim.mat param_struct;
Alternatively, you can modify the block parameters in the model, and use
rsimgetrtp
to create multiple parameter sets:
Use the function
rsimgetrtp
to get a structure containing parameter information for a model. Store the structure in a variableparam_struct
.param_struct = rsimgetrtp('rapidsim');
Use the function
rsimsetrtpparam
to expand the structure so that it contains more parameter sets. In this case, create two more parameter sets (for a total of three sets).param_struct = rsimsetrtpparam(param_struct,3);
The function converts the
parameters
field to a cell array with three elements. Each element contains information for a single parameter set. By default, the function creates the second and third elements of the cell array by copying the first element. Therefore, the parameter sets use the same parameter values.Change the values of block parameters or workspace variables. For example, change the value of the variable
w
from70
to72
.w = 72;
Use
rimsgetrtp
to get another structure containing parameter information. Store the structure in a temporary variablertp_temp
.rtp_temp = rsimgetrtp('rapidsim');
Assign the value of the
parameters
field ofrtp_temp
to the structureparam_struct
as a second parameter set.param_struct.parameters{2} = rtp_temp.parameters;
Change the value of the variable
w
from72
to75
.w = 75;
Use
rimsgetrtp
to get another structure containing parameter information. Then, assign the value of theparameters
field toparam_struct
as a third parameter set.rtp_temp = rsimgetrtp('rapidsim'); param_struct.parameters{3} = rtp_temp.parameters;
Save the structure containing the parameter set information to a MAT-file.
save rapidsim.mat param_struct;
For more information on how to specify each parameter set when you run the simulations, see Change Block Parameters for an RSim Simulation.
Create a MAT-File for a From File Block
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 data format expected by that block. For example, if you are using a matrix as an input for the MAT file, this cannot be different from the matrix size for the executable.
To create a MAT-file for a From File block:
For array format data, in the workspace create a matrix 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 other supported data types, such as
int16
or fixed-point, the time data points must be of typedouble
, just as for array format data. However, the sample data can be of any dimension.For more information on setting up the input data, see the block description of From File.
Save the matrix to a MAT-file.
The following example saves the matrix
var_matrix
to the MAT-filerapidsim.mat
in Version 7.3 format.save '-v7.3' rapidsim.mat var_matrix;
Using a command-line option, you can then specify that MAT-file as input for rapid simulations.
Create a MAT-File for an Inport Block
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 the following procedure, step 4. For more information, see Loading Data Arrays to Root-Level Inputs. |
Signal-and-time structure |
For an example, see Signal-and-time structure in the following procedure, step 4. For more information on this format, see Loading Data Structures to Root-Level Inputs. |
Per-port structure |
For an example, see Per-port structure in the following procedure, step 4. For more information, see Loading Data Structures to Root-Level Inputs. |
To create a MAT-file for an Inport block:
Choose one of the preceding data file formats.
Update Inport block parameter settings and specifications to match specifications of the data to be supplied by the MAT-file.
By default, the Inport block inherits parameter settings from downstream blocks. To import data from an external MAT-file, 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, clear block parameter Interpolate data or modify the parameter so that it is set to a nonempty value. Interpolation requires time data.For descriptions of the preceding block parameters, see Inport.
Build an RSim executable program for the model. The 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 the model executable expects.
Create the MAT-file that provides the source data for the rapid simulations. You can create the MAT-file from a workspace variable. Using the specifications in the preceding format comparison table, create the workspace variables for your simulations.
An example of each format follows:
Single time/data matrix
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];
Signal-and-time structure
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];
Per-port structure
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
This example saves the workspace variable
var_matrix
to MAT-filerapidsim_matrix.mat
.save rapidsim_matrix.mat var_matrix;
Signal-and-time structure
The following example saves the workspace structure variable
var_single_struct
to the MAT-filerapidsim_single_struct.mat
.save rapidsim_single_struct.mat var_single_struct;
Per-port structure
To order data when saving per-port structure variables to a single MAT-file, use the
save
command-append
option. Be sure to append the data in the order that the model expects it.This example saves the workspace variables
Inb1
,Inb2
, andInb3
to MAT-filerapidsim_multi_struct.mat
.save rapidsim_multi_struct.mat Inb1; save rapidsim_multi_struct.mat Inb2 -append; save rapidsim_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 variablesv1
,v2
, andv3
, in that order, the order of the variables in the MAT-file could bev2 v1 v3
.Using a command-line option, you can then specify the MAT-files as input for rapid simulations.