Main Content

Simulink.playback.createSignals

Create Simulink.playback.Signal object to add to Playback block

Since R2024a

    Description

    Add Data from Workspace

    example

    pbSig = Simulink.playback.createSignals("workspace","Variables",vars) creates a Simulink.playback.Signal object that contains the signal or signals from the workspace specified by vars.

    To programmatically add data to the Playback block, use the set_param function to set the Simulink.playback.Signal object as the value of the Signals parameter of the Playback block. For example, add signals with the variable names x1 and x2 from the workspace to the Playback block.

    pbSig  = Simulink.playback.createSignals("workspace","Variables",["x1","x2"]);
    set_param("PlaybackModel/Playback","Signals",pbSig);

    pbSig = Simulink.playback.createSignals("workspace","Variables",vars,Name=Value) creates a Simulink.playback.Signal object and specifies how data is added to the Playback block using one or more name-value arguments.

    Add Data from File

    example

    pbSig = Simulink.playback.createSignals(filename) creates a Simulink.playback.Signal object that contains all signals in the file specified by filename.

    pbSig = Simulink.playback.createSignals(filename,"Variables",vars) creates a Simulink.playback.Signal object that contains the signal or signals specified by vars.

    pbSig = Simulink.playback.createSignals(___,Name=Value) creates a Simulink.playback.Signal object that contains all or some of the signals in the file filename and specifies how data is added to the Playback block using one or more name-value arguments.

    Add Data from Simulation Data Inspector

    pbSig = Simulink.playback.createSignals("sdi","RunID",runID) creates a Simulink.playback.Signal object that contains all the signals in the Simulation Data Inspector run specified by runID.

    pbSig = Simulink.playback.createSignals("sdi","RunName",runName) creates a Simulink.playback.Signal object that contains all the signals in the Simulation Data Inspector run specified by runName.

    pbSig = Simulink.playback.createSignals(___,"Variables",vars) creates a Simulink.playback.Signal object that contains one or more signals specified by vars from a Simulation Data Inspector run.

    pbSig = Simulink.playback.createSignals(___,Name=Value) creates a Simulink.playback.Signal object that contains all or some of the signals in a Simulation Data Inspector run and specifies how data is added to the Playback block using one or more name-value arguments.

    Examples

    collapse all

    The PlaybackRecord model contains an empty Playback block connected to a Record block. Using the set_param function, you can programmatically add new signals to the Playback block by setting the value of the Signals parameter to a Simulink.playback.Signal object that contains information about the signals you want to add to the block. Once you add data, the Playback block loads external data into the model during simulation. The Record block logs output data from the Playback block.

    mdl = "PlaybackRecord";
    open_system(mdl)

    The PlaybackRecord model

    Create two timeseries objects in the workspace. The timeseries object ts1 stores data for a sine wave and has the signal name Sine. The timeseries object ts2 stores data for a cosine wave and has the signal name Cosine.

    time = 0:0.1:10;
    ts1 = timeseries(sin(time),"Name","Sine");
    ts2 = timeseries(cos(time),"Name","Cosine");

    Use the Simulink.playback.createSignals function to create a Simulink.playback.Signal object that contains the workspace variables ts1 and ts2.

    wkspSigs = Simulink.playback.createSignals("workspace","Variables",["ts1","ts2"]);

    To add the workspace data to the Playback block, use the set_param function to apply the Simulink.playback.Signal object to the Signals parameter of the Playback block.

    set_param("PlaybackRecord/Playback","Signals",wkspSigs)

    In the model, connect the second port of the Playback block to the Record block. The Playback block displays the names of the added signals. To load the workspace data from the Playback block into your model, run the simulation.

    The PlaybackRecord model showing the Cosine and Sine signals added to the block

    To get information about the signals added to the Playback block, use the get_param function. For example, get information about the Simulink.playback.Signal object that contains data for the Sine signal.

    sigsInfo = get_param("PlaybackRecord/Playback","Signals");
    sineSigInfo = sigsInfo(2)
    sineSigInfo = 
      Signal with properties:
    
           Source: 'workspace'
         IsLinked: 1
             Name: 'ts1'
          RunName: ''
        PortIndex: '2'
    
    

    The PlaybackRecord model contains an empty Playback block connected to a Record block. Using the set_param function, you can programmatically add new signals to the Playback block by setting the value of the Signals parameter to a Simulink.playback.Signal object that contains information about the signals you want to add to the block. Once you add data, the Playback block loads external data into the model during simulation. The Record block logs output data from the Playback block.

    mdl = "PlaybackRecord";
    open_system(mdl)

    The PlaybackRecord model

    The Microsoft Excel® file TempDataFile contains two sets of indoor and outdoor temperature data. Use the Simulink.playback.createSignals function to create a Simulink.playback.Signal object that contains all of the signals from the TempDataFile file.

    tempSigs = Simulink.playback.createSignals("TempDataFile.xlsx");

    To add the temperature data to the Playback block, use the set_param function to apply the Simulink.playback.Signal object to the Signals parameter of the Playback block.

    set_param("PlaybackRecord/Playback","Signals",tempSigs)

    In the model, connect all of the Playback block ports to the Record block. Alternatively, you can add signal lines programmatically.

    set_param("PlaybackRecord/Record","NumPorts",4)
    add_line(mdl,"Playback/2","Record/2")
    add_line(mdl,"Playback/3","Record/3")
    add_line(mdl,"Playback/4","Record/4")

    The Playback block displays the names of the added signals. To load the temperature data from the Playback block into your model, run the simulation.

    The PlaybackRecord model with all of the signals from the TempDataFile added to the Playback block

    You can also use the set_param function to remove all signals from the Playback block.

    set_param("PlaybackRecord/Playback","Signals",[])

    The PlaybackRecord model with all signals removed from the Playback block

    To add only some signals from a file to the Playback block, you can use the Variables name-value argument to specify which signals to add to the block. For example, you can add only the indoor temperature data.

    1. Create a Simulink.playback.Signal object that contains only the indoor temperature data from the TempDataFile file.

    2. Apply the Simulink.playback.Signal object to the Signals parameter of the Playback block.

    indoorTempSigs = Simulink.playback.createSignals("TempDataFile.xlsx","Variables",["TempIndoor_run1","TempIndoor_run2"]);
    set_param("PlaybackRecord/Playback","Signals",indoorTempSigs)

    In the model, the Playback block displays the names of the two indoor temperature signals that were added to the block and two Ground signals. You can remove the unused ports and signal lines programmatically.

    delete_line(mdl,"Playback/3","Record/3")
    delete_line(mdl,"Playback/4","Record/4")
    set_param("PlaybackRecord/Playback","NumPorts",2)
    set_param("PlaybackRecord/Record","NumPorts",2)

    The PlaybackRecord model with the indoor temperature data added to the block

    A Simulink.SimulationInput object allows you to make changes to a model and run simulations with those changes. These changes are temporarily applied to the model.

    The model PlaybackRecord contains an empty Playback block connected to a Record block. Using the setBlockParameter object function, you can temporarily add new signals to the Playback block by setting the value of the Signals parameter to a Simulink.playback.Signal object that contains information about the signal you want to add to the block for a particular run. Once you add data, the Playback block loads external data into the model during simulation. The Record block logs output data from the Playback block.

    The PlaybackRecord model

    Suppose you want to simulate the model three times with different data added to the Playback block for each run. You can use a SimulationInput object to temporarily add data to the Playback block for each simulation.

    Create three timeseries objects in the workspace.

    • The timeseries object ts1 stores data for a sine wave and has the signal name SineWave.

    • The timeseries object ts2 stores data for a cosine wave and has the signal name CosineWave.

    • The timeseries object ts3 stores data for a random signal and has the signal name Random.

    time = 0:0.1:10;
    ts1 = timeseries(sin(time),time,"Name","SineWave");
    ts2  = timeseries(cos(time),time,"Name","CosineWave");
    ts3  = timeseries(rand(1,length(time)),time,"Name","Random");

    Use the Simulink.playback.createSignals function to create a Simulink.playback.Signal object for each timeseries object in the workspace.

    pbSig_ts1 = Simulink.playback.createSignals("workspace","Variables","ts1");
    pbSig_ts2 = Simulink.playback.createSignals("workspace","Variables","ts2");
    pbSig_ts3 = Simulink.playback.createSignals("workspace","Variables","ts3");

    Create an array of SimulationInput objects to configure three simulations of the PlaybackRecord model.

    for i = 1:3
        simIn(i) = Simulink.SimulationInput("PlaybackRecord");
    end

    To add each signal to the Playback block, use the setBlockParameter function to apply the appropriate Simulink.playback.Signal object to the Signals parameter of the Playback block for each simulation. For example, for the first simulation, specify the value of the Signals parameter to be pbSig_ts1.

    simIn(1) = setBlockParameter(simIn(1),"PlaybackRecord/Playback","Signals",pbSig_ts1);
    simIn(2) = setBlockParameter(simIn(2),"PlaybackRecord/Playback","Signals",pbSig_ts2);
    simIn(3) = setBlockParameter(simIn(3),"PlaybackRecord/Playback","Signals",pbSig_ts3);

    Simulate the model three times using the configuration specified by each element in the array of SimulationInput objects.

    out = sim(simIn);
    [13-Feb-2024 00:52:02] Running simulations...
    [13-Feb-2024 00:52:04] Completed 1 of 3 simulation runs
    [13-Feb-2024 00:52:04] Completed 2 of 3 simulation runs
    [13-Feb-2024 00:52:04] Completed 3 of 3 simulation runs
    

    Because changes made to a model using a Simulink.SimulationInput object are temporary, the Playback block returns to an empty state after running the simulations. To view and inspect the logged data from the three runs, you can use the Simulation Data Inspector.

    Simulink.sdi.view

    A sparkline plot of the three simulations in the Simulation Data Inspector

    Input Arguments

    collapse all

    Name of variables to add to the Playback block, specified as a string or string array. For more information about workspace variables and file formats supported by the Playback block, see Import Data from Workspace or File into Simulation Data Inspector.

    Example: ["x1","x2"]

    Name of file with data to add to the Playback block, specified as a string or character vector. For more information about workspace variables and file formats supported by the Playback block, see Import Data from Workspace or File into Simulation Data Inspector.

    Example: "myData.mat"

    Simulation Data Inspector run identifier, specified as an integer. The Simulation Data Inspector assigns run IDs when it creates runs. You can get the run ID for a run using the Simulink.sdi.getAllRunIDs function or Simulink.sdi.getRunIDByIndex function.

    Example: 540

    Simulation Data Inspector run name, specified as a string or character vector.

    Example: "Run 1: vdp"

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: Islinked=0

    Option for whether data is referenced in source or saved in model when added to the Playback block, specified as one of these values:

    • 1 (true) — The data is referenced in the source linking the signal added to the Playback block to the variable or file that contains the signal data. If the data in the source variable or file changes, that change is reflected in the data added to the Playback block. When linking data, only metadata is stored on the block for each signal.

    • 0 (false) — A copy of the signal data is saved in the model. Changes made to the original data source variable or file are not reflected in the data added to the Playback block.

    Adding data to the Playback block from the Simulation Data Inspector always saves a copy of the data in the model.

    Option to automatically assign ports when data is added to the Playback block, specified as one of these values:

    • "auto" — Ports assigned automatically.

    • "none" — Ports not assigned. If you choose to manually assign ports, you can create ports using the Port Editor and assign ports using the port column of the signal table.

    Output Arguments

    collapse all

    Data to add to Playback block, returned as a Simulink.playback.Signal object. To add signals to the Playback block, use the set_param function to apply pbSig to the Signals parameter of the block.

    Version History

    Introduced in R2024a