Log Selected Variables Programmatically
This example shows how to perform selective logging tasks programmatically. You can enable selective logging on specific variables, set the alias and units for that variable, and plot the results.
To open the model, enter:
openExample('simscape/PermanentMagnetDCMotorExample')Each block has a default variable table that you can retrieve and modify. To get the default variable table for the
Load Torqueblock, enter:table1 = simscape.instrumentation.defaultVariableTable('PermanentMagnetDCMotor/Load Torque')table1 = VariableTable (string ⟼ VariableConfiguration) with 5 variable(s): Name Unit Logging _________ _____ _______ C.w ⟼ <missing> rad/s false R.w ⟼ <missing> rad/s false S ⟼ <missing> N*m false t ⟼ <missing> N*m false w ⟼ <missing> rad/s falseTo select the
tvariable for logging, enter:table1("t").Logging = truetable1 = VariableTable (string ⟼ VariableConfiguration) with 5 variable(s): Name Unit Logging _________ _____ _______ C.w ⟼ <missing> rad/s false R.w ⟼ <missing> rad/s false S ⟼ <missing> N*m false t ⟼ <missing> N*m true w ⟼ <missing> rad/s falseTo change the alias of the
tvariable toMotor Torque, enter:table1("t").Name = "Motor Torque"
table1 = VariableTable (string ⟼ VariableConfiguration) with 5 variable(s): Name Unit Logging ______________ _____ _______ C.w ⟼ <missing> rad/s false R.w ⟼ <missing> rad/s false S ⟼ <missing> N*m false t ⟼ "Motor Torque" N*m true w ⟼ <missing> rad/s falseTo change the units of the
tvariable, enter:table1("t").Unit = "ft*lbf"
table1 = VariableTable (string ⟼ VariableConfiguration) with 5 variable(s): Name Unit Logging ______________ ______ _______ C.w ⟼ <missing> rad/s false R.w ⟼ <missing> rad/s false S ⟼ <missing> N*m false t ⟼ "Motor Torque" ft*lbf true w ⟼ <missing> rad/s falseTo view the
VariableConfigurationobject for thetvariable, enter:table1("t")ans = VariableConfiguration with properties: Name: "Motor Torque" Unit: ft*lbf Logging: onTo set
table1as the variable table for theLoad Torqueblock, enter:simscape.instrumentation.setVariableTable('PermanentMagnetDCMotor/Load Torque',table1)To confirm the updates, use the
simscape.instrumentation.getVariableTablefunction to verify that theLoad Torqueblock has the same settings astable1.simscape.instrumentation.getVariableTable('PermanentMagnetDCMotor/Load Torque')ans = VariableTable (string ⟼ VariableConfiguration) with 5 variable(s): Name Unit Logging ______________ ______ _______ C.w ⟼ <missing> rad/s false R.w ⟼ <missing> rad/s false S ⟼ <missing> N*m false t ⟼ "Motor Torque" ft*lbf true w ⟼ <missing> rad/s falseAfter you run the model, Simscape stores the logged data in the
Simulink.SimulationData.Datasetworkspace variable namedlogsout. To run the model and view thelogsoutvariable, enter:sim('PermanentMagnetDCMotor') logsoutlogsout = Simulink.SimulationData.Dataset 'logsout' with 1 element Name BlockPath ____________ __________________________________ 1 [1x1 Variable] Motor Torque PermanentMagnetDCMotor/Load Torque - Use braces { } to access, modify, or add elements using index.There is only one logged variable, so the index to access the data is
1. To access thelogsoutdata values, use theValuesfunction.logsout{1}.Valuestimeseries Common Properties: Name: 'Motor Torque' Time: [125x1 double] TimeInfo: [1x1 tsdata.timemetadata] Data: [125x1 double] DataInfo: [1x1 tsdata.datametadata] More properties, MethodsTo plot the torque data from the
Load Torqueblock using thelogsoutdata, enter:logsout.plot

See Also
Model Data Editor | Simulation Data
Inspector | About Selective Logging | Log Selected Block Variables | simscape.instrumentation.VariableTable