head
Get top rows of table, timetable, or tall array
Syntax
Description
Examples
Preview Table
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
Preview Contents of Tall Table
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'}
Retrieve Specified Number of Rows in Tall Array
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'}
⋮
Input Arguments
A
— Input array
table | timetable
Input array, specified as a table or timetable.
Data Types:
table
| timetable
k
— Number of rows to extract
scalar
Number of rows to extract, specified as a positive scalar integer. If
A
has fewer than k
rows, then
head
returns all of A
.
Output Arguments
B
— Requested rows
table | timetable
Requested rows, returned as a table or timetable. The data type of
B
is the same as A
.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
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.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)