Skip to main content

Registration is now open for this year's LibreFest! Join us virtually the week of July 13.

Register here
Engineering LibreTexts

8.7: Tables

  • Page ID
    136687
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    What Is a Table?

    A table is a MATLAB data type designed for data that is organized in rows and columns, much like a spreadsheet. Each column in a table is called a variable, and each row usually represents one observation, record, measurement, or item.

    Tables are heterogeneous, which means different columns can store different data types. For example, one column might contain student names, another column might contain ID numbers, and another column might contain GPAs. This makes tables very useful for real-world data.

    A useful way to think about a table is this: a cell array stores mixed data by position, a structure stores mixed data by field name, and a table stores mixed data by named columns.

    Key Idea

    A table is best when your data naturally looks like a spreadsheet: rows are records, columns are variables, and each column has a meaningful name.

     

    Creating a Table from Separate Arrays

    To create a table, first create separate arrays for each column. Then pass those arrays to the table function. The arrays must usually have the same number of rows or elements because each row of the table represents one complete record.

    Example \(\PageIndex{1}\)

    FirstName = ["Dana"; "Adam"; "Kelly"; "Chris"];
    LastName = ["Adams"; "Lambard"; "Simons"; "Baker"]
    BloodType = ["O"; "A"; "B"; "B"];
    Rh = ["-"; "+"; "-"; "+"];
    Systolic = [14; 12; 14; 12];
    Diastolic = [18; 18; 18; 16];

    donorsTable = table(LastName, FirstName, BloodType, Rh, Systolic, Diastolic)

    Solution

    clipboard_e0da2b8082ad68677243f6627833a90f3.png

     

    The result is a table with six variables. MATLAB uses the variable names from the workspace as the table column names. This is one of the reasons tables are easy to read: the column names travel with the data.

     

    Creating a Table with Custom Variable Names

    Sometimes the variable names in the workspace are not the names you want in the table. In that case, you can use the VariableNames option. The following example creates a table with variables named LastName, BloodType, and BloodPressure.

    Example \(\PageIndex{1}\)

    Names = ["Adams"; "Lambard"; "Simons"; "Baker"];
    Types = ["O"; "A"; "B"; "B"];
    Pressures = [14 18; 12 18; 14 18; 12 16];

    donorSummary = table(Names, Types, Pressures, ...
        'VariableNames', {'LastName', 'BloodType', 'BloodPressure'})

    Solution

    clipboard_e20ed03b334f6c85bc61cb6cd4497bfd5.png

     

    Notice that a table variable can itself contain multiple columns. In this example, BloodPressure is a numeric array with two columns.

     

    Accessing Data in a Table

    There are three common ways to access data in a table. This is very important because each method returns something slightly different.

    Syntax

    What MATLAB returns

    Plain-language meaning

    T.VariableName

    The contents of one table variable

    Give me this column by name

    T(:, "VariableName")

    A smaller table

    Give me a table containing this column

    T{:, "VariableName"}

    The contents as an ordinary array

    Open the table and take out the values

    T(row, column)

    A smaller table

    Keep the result as a table

    T{row, column}

    The data inside the table cells

    Extract the actual values

    For example, suppose T is a student table.

    Name = ["Ana"; "Ben"; "Mia"];
    ID = [101; 102; 103];
    GPA = [3.7; 3.2; 3.9];
    
    T = table(Name, ID, GPA);

    To get the GPA values as a numeric array, use dot notation:

    allGPAs = T.GPA

    To get a smaller table that contains only the GPA column, use parentheses:

    gpaTable = T(:, "GPA")

    To get the GPA values using curly braces, use:

    gpaValues = T{:, "GPA"}

    The difference is similar to the difference between parentheses and curly braces for cell arrays. Parentheses keep the table structure. Curly braces extract the contents.

    Common Mistake

    If you want a table as the result, use parentheses. If you want the values inside the table, use curly braces or dot notation.

     

    Adding a New Variable to a Table

    You can add a new column to a table using dot notation. For example, suppose a table stores student quiz scores and exam scores. We can calculate an average and store it as a new table variable.

    Example \(\PageIndex{1}\)

    Name = ["Ana"; "Ben"; "Mia"];
    Quiz = [88; 75; 92];
    Exam = [91; 80; 95];

    grades = table(Name, Quiz, Exam);
    grades.Average = (grades.Quiz + grades.Exam)/2

    Solution

    clipboard_eb097516ba4dafe0f7556dbcbb16ec814.png

     

    This adds a new variable named Average to the table. This is one of the nicest features of tables: calculated results can be stored right next to the original data.

     

    Selecting Rows Based on a Condition

    Tables work very well with logical indexing. For example, we can select only the students whose GPA is greater than or equal to 3.5.

    highGPAStudents = T(T.GPA >= 3.5, :)

    Read this line as: from table T, return the rows where GPA is greater than or equal to 3.5, and return all columns.

    The colon in the column position means all columns, just like it does for matrices and cell arrays.

     

    Tables and Files

    Tables are especially useful when working with spreadsheet-style files. MATLAB includes functions such as readtable and writetable for reading and writing table data.

    students = readtable("students.xlsx");
    writetable(students, "students_updated.xlsx");

    You do not need to master file input and output in this chapter, but it is useful to know that tables connect naturally to Excel files, CSV files, and other data files. This is one reason tables are commonly used for data analysis.

     

     


    8.7: Tables is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?