Main Content

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 (model_P)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 parametersCall the rsimgetrtp function to get the global parameter structure and then save it to a MAT-file.
Provided by a From File blockCreate a MAT-file that a From File block can read.
Provided by an Inport blockCreate a MAT-file that adheres to one of the three data file formats that the Inport block can read.
Provided by a From Workspace blockCreate 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),

  1. Get the structure by calling the function rsimgetrtp.

  2. 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 (model_P) for a model by calling the function rsimgetrtp.

param_struct = rsimgetrtp('model')
ArgumentDescription
modelThe 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.

FieldDescription
modelChecksumA 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.
parametersA structure that contains the model's global parameters.

The parameter structure contains this information.

FieldDescription
dataTypeNameThe name of the parameter data type, for example, double
dataTypeIDInternal data type identifier used by the code generator
complexThe value 0 if real; 1 if complex
dtTransIdxInternal data index used by the code generator
valuesA vector of the parameter values associated with this structure
mapThis 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:

  1. Use the function rsimgetrtp to get a structure containing parameter information for the model. Store the structure in a variable param_struct.

    param_struct = rsimgetrtp('rapidsim');
    

    The parameters field of the structure is a substructure that contains parameter information. The values field of the parameters substructure contains the numeric values of the parameters that you can tune during execution of the simulation code.

  2. 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.

  3. 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];
  4. 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:

  1. Use the function rsimgetrtp to get a structure containing parameter information for a model. Store the structure in a variable param_struct.

    param_struct = rsimgetrtp('rapidsim');
    

  2. 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.

  3. Change the values of block parameters or workspace variables. For example, change the value of the variable w from 70 to 72.

    w = 72;

  4. Use rimsgetrtp to get another structure containing parameter information. Store the structure in a temporary variable rtp_temp.

    rtp_temp = rsimgetrtp('rapidsim');

  5. Assign the value of the parameters field of rtp_temp to the structure param_struct as a second parameter set.

    param_struct.parameters{2} = rtp_temp.parameters;

  6. Change the value of the variable w from 72 to 75.

    w = 75;

  7. Use rimsgetrtp to get another structure containing parameter information. Then, assign the value of the parameters field to param_struct as a third parameter set.

    rtp_temp = rsimgetrtp('rapidsim');
    param_struct.parameters{3} = rtp_temp.parameters;

  8. 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:

  1. 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 type double, 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.

  2. Save the matrix to a MAT-file.

    The following example saves the matrix var_matrix to the MAT-file rapidsim.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.

FormatDescription
Single time/data matrix
  • Least flexible.

  • One variable.

  • Two or more columns. Number of columns must equal the sum of the dimensions of all root Inport blocks plus 1. First column must contain monotonically increasing time points. Other columns contain data points that correspond to the time point in a given row.

  • Data of type double.

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
  • More flexible than the single time/data matrix format.

  • One variable.

  • Must contain two top-level fields: time and signals. The time field contains a column vector of the simulation times. The signals field contains an array of substructures, each of which corresponds to an Inport block. The substructure index corresponds to the Inport block number. Each signals substructure must contain a field named values. The values field must contain an array of inputs for the corresponding Inport block, where each input corresponds to a time point specified by the time field.

  • If the time field is set to an empty value, clear the check box for the Inport block Interpolate data parameter.

  • Data type must match Inport block settings.

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
  • Most flexible

  • Multiple variables. Number of variables must equal the number of Inport blocks.

  • Consists of a separate structure-with-time or structure-without-time for each Inport block. Each Inport block data structure has only one signals field. To use this format, in the Input text field, enter the names of the structures as a comma-separated list, in1, in2,..., inN, where in1 is the data for your model's first port, in2 for the second port, and so on.

  • Each variable can have a different time vector.

  • If the time field is set to an empty value, clear the check box for the Inport block Interpolate data parameter.

  • Data type must match Inport block settings.

  • To save multiple variables to the same data file, you must save them in the order expected by the model, using the -append option.

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:

  1. Choose one of the preceding data file formats.

  2. 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.

  3. 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.

  4. 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);
  5. Save the workspace variables to a MAT-file.

    Single time/data matrix

    This example saves the workspace variable var_matrix to MAT-file rapidsim_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-file rapidsim_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, and Inb3 to MAT-file rapidsim_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 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.