Skip to main content
Engineering LibreTexts

14.1: Table Data Type

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\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}}\)

    By Adam L. Lambert, "Zero to Matlab", section 12.1

    The "table" data type is only available in MATLAB; it is not implemented in Octave.

    Of the complex data structures, tables are probably the easiest to learn. You can think of a table as a spreadsheet. It looks a lot like a 2-D array, but the columns are labeled with text. Tables are the default structure used when you import data (such as a .CSV file) through the GUI.
    Let's build a simple table to see how they work.
    When creating a table in MATLAB, the arrays must be in column format. With that in mind, let's make up a sample data set. I'm just going to do this in the Command Window, but feel free to use a script.
    >>Temp=[100; 200; 300; 400; 500]
    Note the semicolons in the array, this vertical concatenation creates a column vector.
    >>Rate = [2.1; 2.5; 2.8; 3.4; 3.6]
    Now we can use the table function to create a table and assign it to a variable name.
    >>T = table(Temp,Rate)
    Without a semicolon after the table command, the table should be displayed in the Command Window. It shows a header in addition to the data. You should also see a new variable, T, in the Workspace of class table. It should have five rows and two columns.

    We can use dot syntax to access individual columns in the table.
    This array can also be indexed to get a single value.

    At this point, we can easily plot the columns of the table using readable syntax.


    Values can be added to the table just like an array. However, MATLAB will automatically extend the rows
    of the other columns and fill them with default values. It will also issue a warning. Try this command.
    >>T.Temp(6) = 600
    Now let's overwrite the rate.

    >>T.Rate(6) = 4.2
    There is one issue that must be pointed out. All of the variable types in a column must match. In other words, you cannot mix doubles and logicals in a single column. If we try to add a char to the end of one of our columns, then we will get an error. Try it out.
    >>T.Temp(7) = 'Hot!'
    That error is a little cryptic huh? In this case, MATLAB sees an array of chars while every other entry is a
    single value. So what happens if we try to assign it a single letter?
    >>T.Temp(7) = 'H'
    Now we have a number (72), but not a number that is relevant to our data. This is the ASCII code that MATLAB uses for the character H. Since MATLAB had already decided to store numbers in that column, it automatically converted the character to a number. You can do this yourself using the uint8 function.

    If we want to store strings in a table, then we need a whole column of strings. The best way to get that is using a cell array, which will be discussed in another section.
    As mentioned at the beginning of the chapter, if you use the GUI to import a data set such as a .CSV file, a table will be the default suggestion. You can choose other variable types, but often a table is the easiest method to script around. Generally speaking, as long as your data is clean, then MATLAB will select the correct variable type for each column.

    This page titled 14.1: Table Data Type is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

    • Was this article helpful?