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 Torque
block, 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 false
To select the
t
variable for logging, enter:table1("t").Logging = true
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 true w ⟼ <missing> rad/s false
To change the alias of the
t
variable 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 false
To change the units of the
t
variable, 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 false
To view the
VariableConfiguration
object for thet
variable, enter:table1("t")
ans = VariableConfiguration with properties: Name: "Motor Torque" Unit: ft*lbf Logging: on
To set
table1
as the variable table for theLoad Torque
block, enter:simscape.instrumentation.setVariableTable('PermanentMagnetDCMotor/Load Torque',table1)
To confirm the updates, use the
simscape.instrumentation.getVariableTable
function to verify that theLoad Torque
block 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 false
After you run the model, Simscape stores the logged data in the
Simulink.SimulationData.Dataset
workspace variable namedlogsout
. To run the model and view thelogsout
variable, enter:sim('PermanentMagnetDCMotor') logsout
logsout = 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 thelogsout
data values, use theValues
function.logsout{1}.Values
timeseries Common Properties: Name: 'Motor Torque' Time: [125x1 double] TimeInfo: [1x1 tsdata.timemetadata] Data: [125x1 double] DataInfo: [1x1 tsdata.datametadata] More properties, Methods
To plot the torque data from the
Load Torque
block using thelogsout
data, enter:logsout.plot
See Also
Model Data Editor | Simulation Data
Inspector | About Selective Logging | Log Selected Block Variables | simscape.instrumentation.VariableTable