how to read multiple csv files with headers?
Mostrar comentarios más antiguos
Hello everyone,
I have few csv files like SS 2012-07-13 to SS 2012-07-30 with a header as the first line.
And the content inside the file is as follows:
Date,Denuder,Htd Line,Impactor,1130 Case,Pump,RPF,Pyro,1135 Case 2012-07-19 23:58:48,95.9,49.2,49.0,31.2,6.80,559.7,450.2,25.1 2012-07-19 23:58:58,94.2,49.2,58.3,31.3,6.80,551.1,441.5,25.1 2012-07-19 23:59:08,92.6,49.1,64.3,31.3,6.80,542.0,432.9,25.1
I want to read these files consecutively using the following code:
close all;clear all;clc;
strDir = 'G:\Shippo 2012\TEKRAN condition\SS\';
fnames = dir(strcat(strDir, '*.csv'));
numfids = length(fnames);
for i = 1:numfids
X = csvread(strcat(strDir, fnames(i).name));
end
But within the loop matlab can not read the data because of the header and is showing the following error:
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==> Date,
Error in ==> csvread at 52
m=dlmread(filename, ',', r, c);
Error in ==> SS_file_daily_plots_m at 9
X = csvread(strcat(strDir, fnames(i).name));
Does anybody have any idea how can I read those csv files simultaneously with the header.
Thank you very much in advance for the assistance.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 18 de Sept. de 2012
1 voto
csvread() uses dlmread() to do its work, and it does that in a way that only permits numeric values even for header lines.
The last of the data lines you show does not have a date like the previous two do. Is that the case for exactly the 3rd (useful) data line, or for only the last data line, or for random data lines ?
The normal recommendation for dealing with csv numeric values is to switch to textscan(), but textscan() is harder to deal with when there are string fields with missing values.
1 comentario
Md. Moklesur Rahman
el 18 de Sept. de 2012
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!