Get bottom rows of table, timetable, or tall array
Create a table that contains 100 rows and five variables.
load patients
T = table(LastName,Gender,Age,Height,Weight);
size(T)
ans = 1×2
100 5
Preview the last eight rows.
T2 = tail(T)
T2=8×5 table
LastName Gender Age Height Weight
_____________ __________ ___ ______ ______
{'Foster' } {'Female'} 30 70 124
{'Gonzales' } {'Male' } 48 71 174
{'Bryant' } {'Female'} 48 66 134
{'Alexander'} {'Male' } 25 69 171
{'Russell' } {'Male' } 44 69 188
{'Griffin' } {'Male' } 49 70 186
{'Diaz' } {'Male' } 45 68 172
{'Hayes' } {'Male' } 48 66 177
Create a tall table and preview the bottom few rows of data.
Create a tall table for the airlinesmall.csv
data set. Select a subset of the variables to work with. Use tail
to extract the last few rows of data.
varnames = {'Year','Month','ArrDelay','DepDelay','UniqueCarrier'}; ds = tabularTextDatastore('airlinesmall.csv','TreatAsMissing','NA',... 'SelectedVariableNames',varnames); 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)
tt = Mx5 tall table Year Month ArrDelay DepDelay UniqueCarrier ____ _____ ________ ________ _____________ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? : : : : : : : : : :
Collect the results into memory to view the data.
last_rows = gather(tt)
Evaluating tall expression using the Local MATLAB Session: - Pass 1 of 1: Completed in 1.6 sec Evaluation completed in 2 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); 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 1 sec Evaluation completed in 1.2 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'}
⋮
A
— Input arrayInput array, specified as a table or timetable.
Data Types: table
| timetable
k
— Number of rows to extractNumber of rows to extract, specified as a positive scalar integer.
If A
has fewer than k
rows,
then tail
returns all of A
.
B
— Requested rowsRequested rows, returned as a table or timetable. The data type
of B
is the same as A
.
This function fully supports tall arrays. For more information, see Tall Arrays.
You can use head
and tail
with
tall arrays of any valid underlying data type (single
, double
, int8
, datetime
, table
,
and so on).
If you are unsure whether the result returned by gather(A)
will
fit in memory, then use gather(head(A))
or gather(tail(A))
.
These commands still fully evaluate the tall array A
,
but only return a small subset of the result in memory.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
You have a modified version of this example. Do you want to open this example with your edits?