Main Content

tail

Get bottom rows of table, timetable, or tall array

Description

example

B = tail(A) returns the last eight rows of table, timetable, or tall array A.

example

B = tail(A,k) returns the last k rows of A.

example

tail(___) displays the last eight, or the last k, rows of A without returning a value.

Examples

collapse all

Create a table by reading data from a spreadsheet. Display the size of the table, showing that it has 1468 rows.

T = readtable("outages.csv","TextType","string");
size(T)
ans = 1×2

        1468           6

Return another table that has the last eight rows of T.

T2 = tail(T)
T2=8×6 table
      Region          OutageTime        Loss     Customers     RestorationTime           Cause       
    ___________    ________________    ______    __________    ________________    __________________

    "West"         2010-12-04 12:09    245.72     1.074e+05    2010-12-07 13:05    "winter storm"    
    "West"         2013-01-05 05:52         0             0    2013-01-05 06:08    "attack"          
    "West"         2010-12-01 04:12    369.43           NaN    2010-12-01 04:28    "energy emergency"
    "West"         2011-11-21 16:51         0             0    2011-11-21 16:55    "attack"          
    "SouthEast"    2011-01-03 05:52       NaN    2.7596e+05    2011-01-06 05:25    "winter storm"    
    "MidWest"      2011-01-02 14:41    364.24    2.8432e+05    2011-01-04 04:43    "winter storm"    
    "SouthEast"    2013-12-20 19:52    2.3096        1038.2    2013-12-20 23:29    "thunder storm"   
    "SouthEast"    2011-09-14 11:55    45.042         11835    2011-09-14 13:28    "equipment fault" 

Create a table from a file with 1468 rows.

T = readtable("outages.csv","TextType","string");
size(T)
ans = 1×2

        1468           6

Display the last three rows. If you do not specify an output argument, tail does not return a value. It only displays the bottom of the table.

tail(T,3)
      Region          OutageTime        Loss     Customers     RestorationTime           Cause      
    ___________    ________________    ______    __________    ________________    _________________

    "MidWest"      2011-01-02 14:41    364.24    2.8432e+05    2011-01-04 04:43    "winter storm"   
    "SouthEast"    2013-12-20 19:52    2.3096        1038.2    2013-12-20 23:29    "thunder storm"  
    "SouthEast"    2011-09-14 11:55    45.042         11835    2011-09-14 13:28    "equipment fault"

Create a tall table. Then extract and view the last eight rows of data.

Create a tall table for the airlinesmall.csv data set. Select a subset of the variables to work with.

varnames = ["Year","Month","ArrDelay","DepDelay","UniqueCarrier"];
ds = tabularTextDatastore("airlinesmall.csv","TreatAsMissing","NA",...
     "SelectedVariableNames",varnames,"TextType","string");
T = tall(ds)
T =

  Mx5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12           "PS"     
    1987     10          8           1           "PS"     
    1987     10         21          20           "PS"     
    1987     10         13          12           "PS"     
    1987     10          4          -1           "PS"     
    1987     10         59          63           "PS"     
    1987     10          3          -2           "PS"     
    1987     10         11          -1           "PS"     
     :        :         :           :              :
     :        :         :           :              :

To extract the last eight rows of data, use tail. The result is a tall table with eight rows.

tt = tail(T)
tt =

  Mx5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

     ?        ?         ?           ?              ?      
     ?        ?         ?           ?              ?      
     ?        ?         ?           ?              ?      
     :        :         :           :              :
     :        :         :           :              :

Collect the data into memory using the gather function. The result is an ordinary table with eight rows.

last_rows = gather(tt)
Evaluating tall expression using the Local MATLAB Session:
- Pass 1 of 1: Completed in 1.2 sec
Evaluation completed in 1.5 sec
last_rows=8×5 table
    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    2008     12         14           1           "DL"     
    2008     12         -8          -1           "DL"     
    2008     12          1           9           "DL"     
    2008     12         -8          -4           "DL"     
    2008     12         15          -2           "DL"     
    2008     12        -15          -1           "DL"     
    2008     12        -12           1           "DL"     
    2008     12         -1          11           "DL"     

Preview the last 20 rows of data in a tall table.

Create a tall table for the airlinesmall.csv data set. Select a subset of the variables to work with, and treat "NA" values as missing data so that datastore replaces them with NaN values. Use tail to view the last 20 rows of data.

varnames = ["Year","Month","ArrDelay","DepDelay","UniqueCarrier"];
ds = tabularTextDatastore("airlinesmall.csv","TreatAsMissing","NA",...
     "SelectedVariableNames",varnames,"TextType","string");
T = tall(ds)
T =

  Mx5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    1987     10          8          12           "PS"     
    1987     10          8           1           "PS"     
    1987     10         21          20           "PS"     
    1987     10         13          12           "PS"     
    1987     10          4          -1           "PS"     
    1987     10         59          63           "PS"     
    1987     10          3          -2           "PS"     
    1987     10         11          -1           "PS"     
     :        :         :           :              :
     :        :         :           :              :
tt = tail(T,20)
tt =

  Mx5 tall table

    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

     ?        ?         ?           ?              ?      
     ?        ?         ?           ?              ?      
     ?        ?         ?           ?              ?      
     :        :         :           :              :
     :        :         :           :              :

Collect the results into memory to view the data.

b20 = gather(tt)
Evaluating tall expression using the Local MATLAB Session:
- Pass 1 of 1: Completed in 0.77 sec
Evaluation completed in 0.88 sec
b20=20×5 table
    Year    Month    ArrDelay    DepDelay    UniqueCarrier
    ____    _____    ________    ________    _____________

    2008     12          0          -4           "CO"     
    2008     12        -16          13           "CO"     
    2008     12         17          -3           "CO"     
    2008     12          3          -5           "CO"     
    2008     12          2           6           "DL"     
    2008     12          6          -2           "DL"     
    2008     12         37          35           "DL"     
    2008     12         -1          -6           "DL"     
    2008     12         39          12           "DL"     
    2008     12         -3          -6           "DL"     
    2008     12         -6          -1           "DL"     
    2008     12         -2           1           "DL"     
    2008     12         14           1           "DL"     
    2008     12         -8          -1           "DL"     
    2008     12          1           9           "DL"     
    2008     12         -8          -4           "DL"     
      ⋮

Input Arguments

collapse all

Input array, specified as a table, timetable, or tall array.

Number of rows to extract, specified as a positive scalar integer. If A has fewer than k rows, then tail returns all of A.

Output Arguments

collapse all

Requested rows, returned as a table, timetable, or tall array. The data type of B is the same as A.

Extended Capabilities

Version History

Introduced in R2016b

expand all