Main Content

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.

  1. To open the model, enter:

    openExample('simscape/PermanentMagnetDCMotorExample')

  2. 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 

  3. 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 

  4. To change the alias of the t variable to Motor 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 

  5. 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 

  6. To view the VariableConfiguration object for the t variable, enter:

    table1("t")
    ans = 
    
      VariableConfiguration with properties:
    
           Name: "Motor Torque"
           Unit: ft*lbf
        Logging: on

  7. Before the changes affect the block, you must enable logging for the block and apply the variable table to the block. To enable logging and set table1 as the variable table for the Load Torque block, enter:

    simscape.instrumentation.enableLogging('PermanentMagnetDCMotor/Load Torque')
    simscape.instrumentation.setVariableTable('PermanentMagnetDCMotor/Load Torque',table1)

  8. To confirm the updates, use the simscape.instrumentation.getVariableTable function to verify that the Load Torque block has the same settings as table1.

    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 

  9. After you run the model, Simscape stores the logged data in the Simulink.SimulationData.Dataset workspace variable named logsout. To run the model and view the logsout 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.

  10. There is only one logged variable, so the index to access the data is 1. To access the logsout data values, use the Values 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

  11. To plot the torque data from the Load Torque block using the logsout data, enter:

    logsout.plot

    Torque data from logsout workspace variable shows a step change.

See Also

| | |