Main Content

Rename and Describe Table Variables

Tables, which hold data in column-oriented variables, provide properties that can store more descriptive information about the data. For instance, the variable names are stored in a property, so if you want to rename variables to be more descriptive, you can change a table property to do so. This example shows how to access and change table properties, including the names, descriptions, and units of table variables. The example also shows how to produce a table summary to view these properties with statistics about the data in the table variables.

Create Table from Sample Data

Create a table using a subset of the sample patient data from the file patients.mat.

load patients.mat
BloodPressure = [Systolic Diastolic];
LastName = string(LastName);
T = table(LastName,Age,Height,Weight,Smoker,BloodPressure)
T=100×6 table
     LastName     Age    Height    Weight    Smoker    BloodPressure
    __________    ___    ______    ______    ______    _____________

    "Smith"       38       71       176      true       124     93  
    "Johnson"     43       69       163      false      109     77  
    "Williams"    38       64       131      false      125     83  
    "Jones"       40       67       133      false      117     75  
    "Brown"       49       64       119      false      122     80  
    "Davis"       46       68       142      false      121     70  
    "Miller"      33       64       142      true       130     88  
    "Wilson"      40       68       180      false      115     82  
    "Moore"       28       68       183      false      115     78  
    "Taylor"      31       66       132      false      118     86  
    "Anderson"    45       68       128      false      114     77  
    "Thomas"      42       66       137      false      115     68  
    "Jackson"     25       71       174      false      127     74  
    "White"       39       72       202      true       130     95  
    "Harris"      36       65       129      false      114     79  
    "Martin"      48       71       181      true       130     92  
      ⋮

Access Table Properties

A table has properties that you can use to describe the table as a whole as well as its individual variables.

A table stores its properties in a Properties object. To access the properties of a table, use dot notation.

T.Properties
ans = 
  TableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'LastName'  'Age'  'Height'  'Weight'  'Smoker'  'BloodPressure'}
           VariableTypes: ["string"    "double"    "double"    "double"    "logical"    "double"]
    VariableDescriptions: {}
           VariableUnits: {}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

You can also use dot notation to access a specific property. For example, access the property that stores the array of variable names.

T.Properties.VariableNames
ans = 1x6 cell
    {'LastName'}    {'Age'}    {'Height'}    {'Weight'}    {'Smoker'}    {'BloodPressure'}

Rename Table Variables

Variable names are most useful when they are descriptive. So, you might want to rename variables in your table.

The recommended way to rename variables is to use the renamevars function. For example, rename the LastName variable of T to PatientName.

T = renamevars(T,"LastName","PatientName")
T=100×6 table
    PatientName    Age    Height    Weight    Smoker    BloodPressure
    ___________    ___    ______    ______    ______    _____________

    "Smith"        38       71       176      true       124     93  
    "Johnson"      43       69       163      false      109     77  
    "Williams"     38       64       131      false      125     83  
    "Jones"        40       67       133      false      117     75  
    "Brown"        49       64       119      false      122     80  
    "Davis"        46       68       142      false      121     70  
    "Miller"       33       64       142      true       130     88  
    "Wilson"       40       68       180      false      115     82  
    "Moore"        28       68       183      false      115     78  
    "Taylor"       31       66       132      false      118     86  
    "Anderson"     45       68       128      false      114     77  
    "Thomas"       42       66       137      false      115     68  
    "Jackson"      25       71       174      false      127     74  
    "White"        39       72       202      true       130     95  
    "Harris"       36       65       129      false      114     79  
    "Martin"       48       71       181      true       130     92  
      ⋮

Another way to rename variables is to access the T.Properties.VariableNames property. For example, rename the BloodPressure variable.

T.Properties.VariableNames("BloodPressure") = "BP"
T=100×6 table
    PatientName    Age    Height    Weight    Smoker        BP    
    ___________    ___    ______    ______    ______    __________

    "Smith"        38       71       176      true      124     93
    "Johnson"      43       69       163      false     109     77
    "Williams"     38       64       131      false     125     83
    "Jones"        40       67       133      false     117     75
    "Brown"        49       64       119      false     122     80
    "Davis"        46       68       142      false     121     70
    "Miller"       33       64       142      true      130     88
    "Wilson"       40       68       180      false     115     82
    "Moore"        28       68       183      false     115     78
    "Taylor"       31       66       132      false     118     86
    "Anderson"     45       68       128      false     114     77
    "Thomas"       42       66       137      false     115     68
    "Jackson"      25       71       174      false     127     74
    "White"        39       72       202      true      130     95
    "Harris"       36       65       129      false     114     79
    "Martin"       48       71       181      true      130     92
      ⋮

Change Other Properties

To change any other table property, you must use dot notation. In general, you can use the other properties to annotate the table with information that describes it or the variables.

For example, add a description for the table as a whole. Assign a string to the Description property. Also, add units that are associated with the table variables. Assign a string array of the units to the VariableUnits property. While the property stores a cell array of character vectors, you can assign values to it using a string array. An individual empty string within the string array indicates that the corresponding variable does not have units.

T.Properties.Description = "Table of Data for 100 Patients";
T.Properties.VariableUnits = ["","yr","in","lbs","","mm Hg"];
T.Properties
ans = 
  TableProperties with properties:

             Description: 'Table of Data for 100 Patients'
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
           VariableTypes: ["string"    "double"    "double"    "double"    "logical"    "double"]
    VariableDescriptions: {}
           VariableUnits: {''  'yr'  'in'  'lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

You can also assign values by indexing into properties. For example, add descriptions for the PatientName and BP variables only. You can index by name or by the position a variable has in the table.

T.Properties.VariableDescriptions(1) = "Patient last name";
T.Properties.VariableDescriptions("BP") = "Systolic/Diastolic";
T.Properties
ans = 
  TableProperties with properties:

             Description: 'Table of Data for 100 Patients'
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
           VariableTypes: ["string"    "double"    "double"    "double"    "logical"    "double"]
    VariableDescriptions: {'Patient last name'  ''  ''  ''  ''  'Systolic/Diastolic'}
           VariableUnits: {''  'yr'  'in'  'lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

Delete Property Values

You cannot delete table properties. However, you can delete the values stored in table properties.

Remove the description for the LastName variable. The descriptions are text, so remove it by assigning an empty string as the new description.

T.Properties.VariableDescriptions(1) = "";
T.Properties
ans = 
  TableProperties with properties:

             Description: 'Table of Data for 100 Patients'
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
           VariableTypes: ["string"    "double"    "double"    "double"    "logical"    "double"]
    VariableDescriptions: {''  ''  ''  ''  ''  'Systolic/Diastolic'}
           VariableUnits: {''  'yr'  'in'  'lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

Remove all the descriptions in VariableDescriptions. To remove all the values stored in a table property, assign an empty array.

  • If the property stores text in a cell array, assign {}.

  • If the property stores numeric or other types of values in an array, assign [].

T.Properties.VariableDescriptions = {};
T.Properties
ans = 
  TableProperties with properties:

             Description: 'Table of Data for 100 Patients'
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
           VariableTypes: ["string"    "double"    "double"    "double"    "logical"    "double"]
    VariableDescriptions: {}
           VariableUnits: {''  'yr'  'in'  'lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

For the next section, add variable descriptions back to T.

T.Properties.VariableDescriptions = ["Patient name","","","","True if patient smokes","Systolic and diastolic readings"];
T.Properties
ans = 
  TableProperties with properties:

             Description: 'Table of Data for 100 Patients'
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'PatientName'  'Age'  'Height'  'Weight'  'Smoker'  'BP'}
           VariableTypes: ["string"    "double"    "double"    "double"    "logical"    "double"]
    VariableDescriptions: {'Patient name'  ''  ''  ''  'True if patient smokes'  'Systolic and diastolic readings'}
           VariableUnits: {''  'yr'  'in'  'lbs'  ''  'mm Hg'}
      VariableContinuity: []
                RowNames: {}
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

Summarize Table Variable Data and Properties

You can produce a table summary to view its properties with statistics about each variable. To produce this summary, use the summary function. The summary displays the description of the table and the descriptions and units for each variable. The summary also displays statistics for table variables whose data types support the required calculations.

summary(T)
T: 100x6 table

Description: Table of Data for 100 Patients

Variables:

    PatientName: string (Patient name)
    Age: double (yr)
    Height: double (in)
    Weight: double (lbs)
    Smoker: logical (34 true, True if patient smokes)
    BP: 2-column double (mm Hg, Systolic and diastolic readings)

Statistics for applicable variables:

                   NumMissing      Min         Median         Max          Mean            Std    

    PatientName        0                                                                          
    Age                0            25              39         50         38.2800         7.2154  
    Height             0            60              67         72         67.0700         2.8365  
    Weight             0           111        142.5000        202             154        26.5714  
    BP(:,1)            0           109             122        138        122.7800         6.7128  
    BP(:,2)            0            68         81.5000         99         82.9600         6.9325  

You can also store the summary in a structure instead of displaying it.

S = summary(T)
S = struct with fields:
    PatientName: [1x1 struct]
            Age: [1x1 struct]
         Height: [1x1 struct]
         Weight: [1x1 struct]
         Smoker: [1x1 struct]
             BP: [1x1 struct]

Each field of S contains a description of a variable of T.

S.BP
ans = struct with fields:
           Size: [100 2]
           Type: 'double'
    Description: 'Systolic and diastolic readings'
          Units: 'mm Hg'
     Continuity: []
     NumMissing: [0 0]
            Min: [109 68]
         Median: [122 81.5000]
            Max: [138 99]
           Mean: [122.7800 82.9600]
            Std: [6.7128 6.9325]

See Also

| |

Related Topics