matlabtoolboxundefined-function

Undefined function 'head' in Matlab


I have encountered a strange problem. I receive this error while running my code:

Undefined function 'head' for input arguments of type 'table'.

filename = 'C:\\Users\\farazpc.ir\\Downloads\\Telegram 
Desktop\\MainDataset.csv';
m = readtable(filename);
h = head(m,500);

Although I have checked with ver and which commands and I have this function and I have tried to set a path for this method from Home in Matlab then set path part. I followed the instructions from this link:

https://www.mathworks.com/help/matlab/matlab_prog/calling-functions.html

Here is the path for the head method:

 which head

 H:\signal matlab\toolbox\matlab\bigdata\@tall\head.m  % tall method

My Matlab version is 2016b. I am really confused and need help. Thanks in advance.


Solution

  • The function head which you expect is the tabular method, introduced in MATLAB R2016b (as stated at the bottom of the docs page).

    My guess would be you're using a MATLAB version older than R2016b.

    Across all toolboxes, there are in fact 4 head functions (as of R2017b), you can get the available functions listed by using the -all argument for which:

    >> which head -all
    C:\Program Files\MATLAB\R2017b\toolbox\matlab\bigdata\@tall\head.m              % tall method
    C:\Program Files\MATLAB\R2017b\toolbox\matlab\datatypes\@tabular\head.m         % tabular method
    C:\Program Files\MATLAB\R2017b\toolbox\distcomp\parallel\@codistributed\head.m  % codistributed method
    C:\Program Files\MATLAB\R2017b\toolbox\distcomp\gpu\@gpuArray\head.m            % gpuArray method
    

    Because you don't have the tabular method, there is no defined function head for input type table - only for tall array data types (or anything else you have the toolbox for).


    Workaround

    A quick workaround is to just use indexing

    h = m( 1:500, : ); % Get first 500 rows of m, for all columns
    

    If your table might not have enough rows, add some protection:

    h = m( 1:min(500,size(h,1)), : ); % Get first 500 rows (or all rows if <500)
    

    This is basically what the desired head function does anyway...


    As directed in the MATLAB tag wiki (or tag info) on this site, please always specify which release version you're using, it helps diagnose problems exactly like this one!