Main Content

head

Get top rows of array or table

Description

example

head(A) displays the first eight rows of array, table, or timetable A in the Command Window without storing a value.

example

head(A,k) displays the first k rows of A.

example

B = head(___) returns the requested rows of A for either of the previous syntaxes. The data type of B is the same as the data type of A.

Examples

collapse all

Create a matrix with 100 rows, and display the first eight rows of the matrix.

If you do not specify an output argument, head does not return a value. It only displays the top of the matrix.

A = repmat((1:100)',1,4);
head(A)
     1     1     1     1
     2     2     2     2
     3     3     3     3
     4     4     4     4
     5     5     5     5
     6     6     6     6
     7     7     7     7
     8     8     8     8

Create a table from a file with 1468 rows.

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

        1468           6

Display the first three rows. If you do not specify an output argument, head does not return a value. It only displays the top of the table.

head(T,3)
      Region            OutageTime          Loss     Customers       RestorationTime           Cause     
    ___________    ____________________    ______    __________    ____________________    ______________

    "SouthWest"    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    "winter storm"
    "SouthEast"    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    "winter storm"
    "SouthEast"    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    "winter storm"

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 first eight rows of T.

T2 = head(T)
T2=8×6 table
      Region            OutageTime          Loss     Customers       RestorationTime             Cause      
    ___________    ____________________    ______    __________    ____________________    _________________

    "SouthWest"    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    "winter storm"   
    "SouthEast"    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    "winter storm"   
    "SouthEast"    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    "winter storm"   
    "West"         06-Apr-2004 05:44:00    434.81    3.4037e+05    06-Apr-2004 06:10:00    "equipment fault"
    "MidWest"      16-Mar-2002 06:18:00    186.44    2.1275e+05    18-Mar-2002 23:23:00    "severe storm"   
    "West"         18-Jun-2003 02:49:00         0             0    18-Jun-2003 10:54:00    "attack"         
    "West"         20-Jun-2004 14:39:00    231.29           NaN    20-Jun-2004 19:16:00    "equipment fault"
    "West"         06-Jun-2002 19:28:00    311.86           NaN    07-Jun-2002 00:51:00    "equipment fault"

Input Arguments

collapse all

Input data, specified as an array, cell array, table, or timetable.

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

Extended Capabilities

Version History

Introduced in R2016b

expand all