Tune Parameters with MATLAB Language

You use the MATLAB® functions to change block parameters. With these functions, you do not need to set the Simulink® interface to external mode. You also do not need to connect the Simulink interface with the real-time application.

You can download parameters to the real-time application while it is running or between runs. You can change parameters in your real-time application without rebuilding the Simulink model and change them back to their original values. using Simulink Real-Time™ functions.

Note

  • Simulink Real-Time does not support parameters of multiword data types.

  • Parameter access by parameter index will be removed in a future release. Access parameters by parameter name instead.

  • Method names are case-sensitive and must be complete. Property names are not case-sensitive and do not need to be complete, as long as they are unique.

This procedure uses the Simulink model xpcosc. You must have already created and downloaded the real-time application to the default target computer.

  1. In the Command Window, type:

    tg = slrt;
    
    start(tg)

    The target computer displays the following message:

    System: execution started (sample time: 0.001000)
  2. Display a list of parameters. Type:

    tg.ShowParameters = 'on'

    The ShowParameters command displays a list of properties for the target object.

    Target: TargetPC1
       Connected  = Yes
       Application= xpcosc
    .
    .
    .
       NumParameters        = 7
       ShowParameters       = on
       Parameters =
    
          VALUE    TYPE    SIZE    PARAMETER NAME    BLOCK NAME
          1000000  DOUBLE  Scalar  Gain              Gain      
          400      DOUBLE  Scalar  Gain              Gain1     
          1000000  DOUBLE  Scalar  Gain              Gain2     
          0        DOUBLE  Scalar  InitialCondition  Integrator
          0        DOUBLE  Scalar  InitialCondition  Integrator1
          4        DOUBLE  Scalar  Amplitude         Signal Generator
          20       DOUBLE  Scalar  Frequency         Signal Generator
    
  3. Change the gain. For example, to change the Gain1 block, type:

    pt = setparam(tg, 'Gain1', 'Gain', 800)

    The setparam method returns a structure that stores the source information, the previous value, and the new value.

    When you change parameters, the changed parameters in the target object are downloaded to the real-time application. The development computer displays the following message:

    pt = 
    
           Source: {'Gain1'  'Gain'}
        OldValues: 400
        NewValues: 800

    The real-time application runs. The plot frame updates the signals for the active scopes.

  4. Stop the real-time application. In the Command Window, type:

    stop(tg)
  5. To reset to the previous values, type:

    pt = setparam(tg, pt.Source{1}, pt.Source{2}, pt.OldValues)
    
    pt = 
    
           Source: {'Gain1'  'Gain'}
        OldValues: 800
        NewValues: 400
    

Related Topics