Effacer les filtres
Effacer les filtres

Best way to read in CSV file with header

10 vues (au cours des 30 derniers jours)
Jared
Jared le 7 Nov 2011
I was looking at textread and textscan, but they seem like they won't be useful in my case. I have a 1010 column file, with headers. I need to ignore the first 10 column (only those columns have headers). The number of rows is variable, but probably at least 1000.
If I didn't have headers the load command seems like it would work fine. I don''t want to manually strip the headers off beforehand, so this won't work either.

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Nov 2011
There is an undocumented textscan() parameter 'headercolumns' to indicate the number of leading columns on the line to skip. Note for this purpose that "column" is determined using the same criteria used to determine "column" for the rest of textscan().
Possibly this HeaderColumns setting is only implemented if you use the undocumented format string '' (the empty string) which only works when all of the (unskipped) columns are numeric.
HL = 3; %for example
HC = 10; %in your case
result = textscan(fid, '', 'HeaderLines', HL, 'HeaderColumns', HC, 'Delimiter', ',');
If you want more control over your columns or do not like using undocumented parameters, then use an explicit format that throws away the unwanted data:
HL = 3; %for example
HC = 10; %in your case
NF = 1000; %1000 desired fields
lineformat = [repmat('%*s',1,HC) repmat('%f',1,NF)];
result = textscan(fid, lineformat, 'HeaderLines', HL, 'Delimiter', ',');

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by