Main Content

Save and Reload Parameters by Using the MATLAB Language

After you load a real-time application that has parameter values, you can save those values to a parameter set file on the target computer. You can then later reload these parameter values to the same real-time application. To ease the process of tuning parameters, use the ParameterSet object workflow. For example code that demonstrates this workflow, see the ParameterSet object.

Note

The parameter set file lets you preserve parameter values outside of the real-time application and model. To apply parameter values from the parameter set file, load the parameter set into the real-time application after you load the application. This action appears in the Edit parameters loop of the workflow.

When your parameter set values are tuned, you can:

  • Export the values from the parameter set to the model by using exportToModel function.

  • Save the ParameterSet object as a MAT file and share this MAT file with other developers.

  • Add the parameter set into the real-time application MLDATX file and set the parameter set as the startup parameter set by using the addParamSet and updateStartupParameterSet functions.

You can save parameters from your real-time application while the real-time application is running or between runs. You can save and restore parameters in your real-time application without rebuilding the Simulink® model. Load parameters to the same real-time application from which you saved the parameter file. If you attempt to load a parameter file to a different real-time application or to a real-time application that has changed since the parameter set was created, the load issues an error.

You can use the syncWithApp function to synchronize an out-of-sync ParameterSet object with the specified real-time application. This function synchronizes the parameter name-value pairs and synchronizes the model checksum saved in the ParameterSet object with the real-time application. After synchronizing, the parameter set that you saved from the original application can be loaded into the most updated application on the target computer.

You save and restore parameters by using the target object methods saveParamSet and loadParamSet.

Requirements:

  • Create a Target object named tg connected to the target computer.

  • Load a real-time application on the target computer.

  • There are parameters to save from the application.

Save Current Set of Real-Time Application Parameters

To save a set of parameters from a real-time application to a parameter set file, use the saveParamSet function. The real-time application can be loaded or running.

Open the Model

This example uses the model slrt_ex_osc_outport. To open this model, in the MATLAB Command Window, type:

open_system('slrt_ex_osc_outport');

Build the Real-Time Application

Select a descriptive file name for the parameters. For example, use the model name in the file name. In the MATLAB Command Window, type:

mdlName = 'slrt_ex_osc_outport';
evalc('slbuild(mdlName)');
tg = slrealtime('TargetPC1');
load(tg,mdlName);

Save the Parameter Set

To save the parameter set to a file, in the MATLAB Command Window, type:

paramSetName = 'outportTypes';
saveParamSet(tg,paramSetName);

The Simulink® Real-Time™ software creates a parameter set file named outportTypes on the target computer.

Close the Model

bdclose("all");

Load Saved Parameters to Real-Time Application

To load a parameter set file of saved parameters to a real-time application, use the loadParamSet function. Load parameters to the same real-time application from which you save the parameter set file. If you attempt to load a parameter file to a different real-time application or to a real-time application that has changed since the parameter set was created, the load issues an error. This example uses the model slrt_ex_osc_outport. You must have a parameters file saved from an earlier run of saveParamSet to perform this procedure.

  1. To open this model, in the MATLAB Command Window, type:

    openExample('slrealtime/SlrtSaveCurrentSetOfRealTimeApplicatioParametersExample');
    open_system('slrt_ex_osc_outport');
  2. From the collection of parameter set files on the target computer, select the one that contains the parameter values to load. To get a list of available parameter set files, use the listParamSet function.

  3. In the Command Window, type:

    % load real-time application 
    mdlName = 'slrt_ex_osc_outport';
    tg = slrealtime('TargetPC1');
    load(tg,mdlName);
    
    % load parameter set file
    paramSetName = 'outportTypes';
    loadParamSet(tg,paramSetName); 
  4. The Simulink Real-Time™ software loads the parameter values into the real-time application. For an example that shows how to get the parameter values from a ParameterSet object and use the object in the loadParamSet function, see loadParamSet.

View or Edit Parameter Values in Parameter Set

To view or edit parameters in a parameter set, use the ParameterSet object workflow. For more information about this workflow, see Save and Reload Parameters by Using the MATLAB Language.

  1. Build the model and load the real-time application.

    openExample('slrealtime/SlrtSaveCurrentSetOfRealTimeApplicatioParametersExample');
    mdlName = 'slrt_ex_osc_outport';
    slbuild(mdlName);
    tg = slrealtime('TargetPC1');
    load(tg,mdlName);
  2. Save the parameter set to a file.

    paramSetName = 'outportTypes';
    saveParamSet(tg,paramSetName);
  3. Import the parameter set into a ParameterSet object on the development computer.

    myParamSet = importParamSet(tg,paramSetName);
  4. Open the ParameterSet in the Simulink Real-Time Parameter Explorer UI. In the explorer, you can view and edit the parameter values in the object.

    explorer(myParamSet);
  5. After tuning the parameters, export the modified parameter set to the target computer and load the parameters into the real-time application.

    exportParamSet(tg,myParamSet);
    loadParamSet(tg,myParamSet.filename);

Add or Update Startup Parameter Set for Application

You can add a ParameterSet object to a real-time application by using the addParamSet function. After adding one or more ParameterSet objects to an application by using the addParamSet function, you can choose which of these parameter sets is loaded into the real-time application on startup by using the updateStartupParameterSet function.

This example loads a real-time application, imports the parameters into a ParameterSet object, adds the ParameterSet object to a real-time application, and selects the parameter set as the startup parameter set for the application.

load(tg,mdlName);
paramSetName = 'outportTypes';
saveParamSet(tg,paramSetName);
myParamSet = importParamSet(tg,paramSetName);
addParamSet(app_object,myParamSet);
updateStartupParameterSet(app_object,myParamSet);

See Also

| | | | | | | | | | | | | | | | |

Related Topics