Main Content

Sweep MATLAB Variables with MATLAB Scripting

You can embed MATLAB® variables in the base workspace with MATLAB commands and use MATLAB language to change their values during execution. In Connected IO mode or accelerator mode, Simulink® transfers the new values to the model that is being simulated. In Run in Kernel mode, Simulink transfers the new values to the real-time application that is running in the kernel mode process.

For this example, your goal is to minimize ringing in the transfer function. For improved performance, you have inlined block parameters. When you inline block parameters, the parameters appear as nontunable constants in the generated code. To make an individual parameter tunable, use a MATLAB variable with a storage class other than Auto to store the parameter in memory.

You can permanently store parameter objects and other external data in a data dictionary.

This procedure uses the square-wave transfer function model sldrtex_inlined. To open this model, in the MATLAB Command Window, type:


First, install the Simulink Desktop Real-Time™ kernel and cd to a working folder.

  1. Open sldrtex_inlined and the Scope block.

    model = 'sldrtex_inlined';
    open_system(fullfile(docroot, 'toolbox', 'sldrt', 'examples', model));
    scname = [model '/Scope'];
  2. In the base workspace, create a parameter object configured to store the parameter as a global variable.

    Dmp = Simulink.Parameter([1 70 10000]);
  3. Replace the Transfer Fcn block parameter Denominator with the parameter object.

    xfername = [model '/Transfer Fcn'];
  4. Start execution with the original Dmp variable value.

  5. Sweep the Dmp variable from 30 to 180 by 30.

    for Val = 30 : 30 : 180
        Dmp.Value = [1 Val 10000];

    The Scope block shows changes at 30-unit intervals. The figures show key changes.

    Val == 30

    Image of scope plot sweep 30

    Val == 90

    Image of scope plot sweep 90

    Val == 180

    Image of scope plot sweep 180

  6. Stop execution.


Related Topics