Rename and Describe Table Variables - MATLAB & Simulink (2024)

Open Live Script

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.matBloodPressure = [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'} 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'} 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'} 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'} 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'} 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'} 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)
Description: Table of Data for 100 PatientsVariables: PatientName: 100x1 string Properties: Description: Patient name Age: 100x1 double Properties: Units: yr 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 Properties: Description: True if patient smokes Values: True 34 False 66 BP: 100x2 double Properties: Units: mm Hg Description: Systolic and diastolic readings Values: Column 1 Column 2 ________ ________ Min 109 68 Median 122 81.5 Max 138 99 

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: [] Min: [109 68] Median: [122 81.5000] Max: [138 99] NumMissing: [0 0]

See Also

table | renamevars | summary

Related Topics

  • Access Data in Tables
  • Add, Delete, and Rearrange Table Variables
  • Add Custom Properties to Tables and Timetables
  • Clean Messy and Missing Data in Tables
  • Calculations When Tables Have Both Numeric and Nonnumeric Data
Rename and Describe Table Variables
- MATLAB & Simulink (2024)

FAQs

How to rename table variables in MATLAB? ›

You can also rename all of the variables in a table by setting its VariableNames property, as in T. Properties. VariableNames = newNames . In that case, newNames must be a string array or a cell array of character vectors.

How to rename a variable in MATLAB? ›

Direct link to this question
  1. function DoStuff(Name)
  2. Name=strcat(Name, 'TheRestOfTheFileName')
  3. load('Folder', Name) %This puts the variable in the workspace with its own name.
  4. NewVar=load('Folder', Name)
  5. NewVar=NewVar. (Name) %This creates a new variable with the data from the old one.
Mar 22, 2019

How do you set variable names in a table in MATLAB? ›

To specify table variable names, use the 'VariableNames' name-value pair argument. For example, you can use 'VariableNames' to specify names when the other input arguments are not workspace variables.

How will you add description for a table in MATLAB? ›

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.

How do you rename a variable in a data table? ›

You can rename a variable in a dataset by changing a value in the names() vector (base R) or by using the rename() function (tidyverse). To rename all the variables, assign a vector of names (which means that for variable names you do not wish to rename, you must specify the existing name).

Can you alter a table variable? ›

Table variables can't be altered after creation. Tables variables can't be used in an INSERT EXEC or SELECT INTO statement.

How do you rename all variables in MATLAB workspace? ›

Rename Variables

The model loads data to the MATLAB base workspace. Open Model Explorer. In the Model Hierarchy pane, select the base workspace. In the Contents pane, right-click the base workspace variable m and select Rename All.

What is the shortcut for rename variable in MATLAB? ›

When you rename a variable or function, if there is more than one reference to that variable or function in the file, MATLAB prompts you to rename all instances by pressing Shift+Enter. You also can rename only the instances from the current cursor location to the end of the file by pressing Alt+Shift+Enter.

How do you preserve variable names in MATLAB? ›

Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname function). To preserve these variable names and row names, set the value of VariableNamingRule to "preserve" .

What are the types of variables in MATLAB table? ›

Variables in the input table or timetable, specified as a string array, character vector, cell array of character vectors, pattern scalar, numeric array, logical array, or function handle.

What are variable names in MATLAB? ›

A valid MATLAB variable name consists of letters, digits, and underscores, such that the first character is a letter, and the length of the name is less than or equal to the value returned by the namelengthmax function. Any name that exceeds namelengthmax is truncated in the varname output.

How to display variable name and value in MATLAB? ›

disp( X ) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “ X = ” before the value. If a variable contains an empty array, disp returns without displaying anything.

How do you add a description to a table? ›

Select the object (table, equation, figure, or another object) that you want to add a caption to. On the References tab, in the Captions group, click Insert Caption. In the Label list, select the label that best describes the object, such as a figure or equation.

How to modify a table in MATLAB? ›

If you want to change any data in the table, you can do so by double-clicking the corresponding cell . If you want to change the data type of any variable, use the drop-down menu. Once you are satisfied with the data you want to import, click Import Selection. In MATLAB, you'll see the table in the Workspace.

How do you write a table description? ›

Tips
  1. Start by saying what information is shown. ...
  2. In the second paragraph give an overview of the most important features of the information.
  3. Be selective and choose the key observations and trends. ...
  4. Divide your observations into paragraphs about different aspects of the data.

How to redefine a variable in MATLAB? ›

You can edit the value of a variable element by clicking the element and typing a new value. Press Enter or click another element to save the change. To return to the parent cell array or structure of an element, go to the View tab and click the Go Up button.

How do you remove variable names from a table in MATLAB? ›

T2 = removevars( T1 , vars ) deletes the table variables specified by vars and copies the remaining variables to T2 . You can specify variables by name, by position, or using logical indices. For example, to remove table variable var3 , use T2 = removevars(T1,'var3') .

How do I rename a variable in Mat file in MATLAB? ›

If you load the mat file into a struct (i.e. the load option with output argument), then your variables become fields of that struct which allows you to rename them (renaming variables themselves is not a fun task). You can then save the struct back out to the mat file with the '-struct' flag.

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6314

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.