0

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.

Masoud Erfani
  • 325
  • 2
  • 12
  • 1
    That looks liek a different fucntion from a different toolbox that is called the same. You are not using `tall` arrays, you are using tables. If that is the only one you have, then it means you dont have the required toolbox – Ander Biguri Oct 17 '18 at 09:59
  • can you try using a simpler filename? I would suggest cd into the directory where the csv is and run readtable('MainDataset.csv');, see if this works around the rpoblem – Yuval Harpaz Oct 17 '18 at 11:13
  • 1
    @YuvalHarpaz That's very unlikely, given that the error is for an undefined function `head`, not a failed file load using `readtable`, a sensible assumption is the table was read successfully... – Wolfie Oct 17 '18 at 11:14
  • head is mistaken for an input argument. it is possible that parsing filename to two or more parts resulted in more than one input argument. – Yuval Harpaz Oct 17 '18 at 11:23
  • Thank you all for your kind responses. The problem was head method was not the one That I thought. – Masoud Erfani Oct 17 '18 at 12:54

1 Answers1

2

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!

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • Thank you for your kind response. Yes, I did not have the head "tabular method" but, my Matlab version is 2016b. – Masoud Erfani Oct 17 '18 at 12:52
  • @m.erfani If you have R2016b then you should have `head` for tables, it is in core MATLAB and doesn't require any toolboxes. You can still use my suggested work-around, but note that your MATLAB installation may have an issue if what you say is true! – Wolfie Oct 17 '18 at 13:14
  • `head` was added first only for `tall` arrays, the `table` implementation was only added later. – Edric Oct 17 '18 at 18:06
  • Is there a way to add updated methods to R2016b without Installing new version? – Masoud Erfani Oct 18 '18 at 22:15
  • @m.erfanim nope, that's the incentive to get the new version. Not worth it for `head`, the indexing option I've shown is literally the same functionality – Wolfie Oct 18 '18 at 23:22
  • @Wolfie Yes, I used your way and it works fine and thanks to you for that. Upgrading to the new version is just a little bothering:) Thank you anyway. – Masoud Erfani Oct 22 '18 at 13:15