Main Content

Modify Units, Descriptions, and Table Variable Names

This example shows how to access and modify table properties for variable units, descriptions, and names. You also can edit these property values using the Variables Editor.

Load Sample Data

Load the sample patients data and create a table. T.LastName is a cell array of character vectors, so convert to a string array. The table T 100 rows and 6 variables. The sixth variable, BloodPressure, is a 100-by-2 numeric array.

load patients
BloodPressure = [Systolic Diastolic];

T = table(LastName,Age,Height,Weight,Smoker,BloodPressure);
T.LastName = string(T.LastName)
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  
      ⋮

Add Variable Units

Specify units for each variable in the table by modifying the table property, VariableUnits. While the property is a cell array of character vectors, you can assign values to it using a string array.

T.Properties.VariableUnits = ["","Yrs","In","Lbs","","mm Hg"];

An individual empty string within the string array indicates that the corresponding variable does not have units.

Add a Variable Description for a Single Variable

Add a variable description for the variable, BloodPressure. Assign text to the element of VariableDescriptions that contains the description for BloodPressure.

T.Properties.VariableDescriptions("BloodPressure") = "Systolic/Diastolic";

You can use the variable name, BloodPressure, or the numeric index of the variable, 6, to index into VariableDescriptions.

Summarize the Table

View the data type, description, units, and other descriptive statistics for each variable by using summary to summarize the table.

summary(T)
Variables:

    LastName: 100x1 string

    Age: 100x1 double

        Properties:
            Units:  Yrs
        Values:

            Min          25   
            Median       39   
            Max          50   

    Height: 100x1 double

        Properties:
            Units:  In
        Values:

            Min          60   
            Median       67   
            Max          72   

    Weight: 100x1 double

        Properties:
            Units:  Lbs
        Values:

            Min          111  
            Median     142.5  
            Max          202  

    Smoker: 100x1 logical

        Values:

            True        34   
            False       66   

    BloodPressure: 100x2 double

        Properties:
            Units:  mm Hg
            Description:  Systolic/Diastolic
        Values:
                      Column 1    Column 2
                      ________    ________

            Min         109           68  
            Median      122         81.5  
            Max         138           99  

The BloodPressure variable has a description and the Age, Height, Weight, and BloodPressure variables have units.

Change a Variable Name

Change the variable name for the first variable from LastName to PatientName.

T.Properties.VariableNames("LastName") = "PatientName";

Display the first five rows of the table, T.

T(1:5,:)
ans=5×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  

In addition to properties for variable units, descriptions and names, there are table properties for row and dimension names, a table description, and user data.

See Also

| | | | |

Related Topics