MATLAB textscan headerlines

Mark Hughes picture Mark Hughes · Jan 5, 2013 · Viewed 7.1k times · Source

When I try to use headerlines with textscan to skip the first line of the text file, all of my data cells are stored as empty.

fid = fopen('RYGB.txt');
A = textscan(fid, '%s %s %s %f', 'HeaderLines', '1'); 
fclose(fid);

This code gives

1x4 Cell
[] [] [] []

Without the headerlines part and without a first line that needs to be skipped in the text file, the data is read in with no problem. It creates a 1x4 cell with data cells containing all of the information from the text file in columns.

What can I do to to skip the first line of the text file and read my data in normally?

Thanks

Answer

Eitan T picture Eitan T · Jan 5, 2013

I think your problem is that you have specified a string instead of an integer value for HeaderLines. The character '1' is interpreted as its ASCII value, 0x31 (49 decimal), so the first 49 lines are skipped. Your file probably contains 49 lines or less, so everything ends up being discarded. This is why you're getting empty cells.

The solution is to replace '1' with 1 (i.e. remove the quotes), like so:

A = textscan(fid, '%s %s %s %f', 'HeaderLines', 1);

and this should do the trick.