Simulink.SimulationData.DatasetRef - Create Simulink.SimulationData.DatasetRef object - MATLAB (original) (raw)

You can construct and use Simulink.SimulationData.DatasetRef objects to access data for a model that logs to persistent storage. This example shows the basic steps for logging to persistent storage. This example does not represent a realistic situation for logging to persistent storage because it shows a short simulation with small memory requirements.

Open the vdp model.

mdl = "vdp"; open_system(mdl)

In the model, mark the signal x1 for signal logging.

  1. Right-click the signal x1.
  2. Select Log Selected Signals.

Alternatively, you can mark the signal for logging programmatically.

Simulink.sdi.markSignalForStreaming("vdp/x1",1,"on")

Configure the model to log data to persistent storage.

In the Configuration Parameters > Data Import/Export pane:

  1. Select the States parameter.
  2. Select the Log Dataset data to file parameter.
  3. Click OK.

Click Run to simulate the model.

Alternatively, you can select the parameters programmatically using name-value arguments.

sim(mdl,"SaveState","on","LoggingToFile","on")

ans = Simulink.SimulationOutput:

               tout: [87x1 double] 

 SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
       ErrorMessage: [0x0 char] 

Get a list of Dataset object variable names in the out.mat file.

varNames = Simulink.SimulationData.DatasetRef.getDatasetVariableNames("out.mat")

varNames = 1×3 cell {'logsout'} {'xout'} {'yout'}

Create a reference to the logged states data that is stored in out.mat. The variable for the logged states data is xout.

statesLogRef = Simulink.SimulationData.DatasetRef("out.mat","xout")

statesLogRef = Simulink.SimulationData.DatasetRef Characteristics: Location: out.mat (/tmp/Bdoc25a_2864802_2872288/tp2f3363d8/simulink-ex68096319/out.mat) Identifier: xout

Resolved Dataset: 'xout' with 2 elements

   Name  BlockPath 
   ____  _________ 
1  ''    vdp/x1   
2  ''    vdp/x2   

Create a reference to the signal logging data that is stored in out.mat. The variable for the signal logging data is logsout.

sigLogRef = Simulink.SimulationData.DatasetRef("out.mat","logsout")

sigLogRef = Simulink.SimulationData.DatasetRef Characteristics: Location: out.mat (/tmp/Bdoc25a_2864802_2872288/tp2f3363d8/simulink-ex68096319/out.mat) Identifier: logsout

Resolved Dataset: 'logsout' with 2 elements

   Name  BlockPath 
   ____  _________ 
1  x1    vdp/x1   
2  x2    vdp/x2   

Use the numElements function to access the number of elements in the logged states Datasetref object.

numElements(statesLogRef)

Use curly braces to access the first element of the signal logging Datasetref object.

ans = Simulink.SimulationData.Signal Package: Simulink.SimulationData

Properties: Name: 'x1' PropagatedName: '' BlockPath: [1×1 Simulink.SimulationData.BlockPath] PortType: 'outport' PortIndex: 1 Values: [1×1 matlab.io.datastore.SimulationDatastore]

Methods, Superclasses

If you delete the persistent storage MAT file and try to use one the DatasetRef objects, the software returns an error because the file does not exist. The statesLogRef variable still exists, but it is a reference to a Dataset object that is in a file that no longer exists.