Get top 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 first eight rows.
T2 = head(T)
T2=8×5 table
LastName Gender Age Height Weight
____________ __________ ___ ______ ______
{'Smith' } {'Male' } 38 71 176
{'Johnson' } {'Male' } 43 69 163
{'Williams'} {'Female'} 38 64 131
{'Jones' } {'Female'} 40 67 133
{'Brown' } {'Female'} 49 64 119
{'Davis' } {'Female'} 46 68 142
{'Miller' } {'Female'} 33 64 142
{'Wilson' } {'Male' } 40 68 180
Create a tall table and preview the first few rows of data.
Create a tall table for the airlinesmall.csv
data set. Select a subset of the variables to work with. Use head
to extract the first 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 = head(T)
tt = 8x5 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'}
Collect the results into memory to view the data.
t8 = gather(tt)
t8=8×5 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'}
Preview the first 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 head
to view the first 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 = head(T,20)
tt = 20x5 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'} : : : : : : : : : :
Collect the results into memory to view the data.
t20 = gather(tt)
t20=20×5 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'}
1987 10 3 3 {'PS'}
1987 10 2 1 {'PS'}
1987 10 16 15 {'PS'}
1987 10 3 9 {'PS'}
1987 10 39 15 {'PS'}
1987 10 57 32 {'TW'}
1987 10 0 -3 {'TW'}
1987 10 -14 0 {'TW'}
⋮
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
head
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?