trouble importing multiple .csv files with strings in first row
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Amir Ahmadi
le 25 Jan 2019
Réponse apportée : Guillaume
le 25 Jan 2019
Hi all,
I wrote below MATLAB code to import multiple csv files named B00Pr1-0001 to B00Pr1-n in a folder and save them in a cell:
l=dir('B00Pr1-*.csv'); % list .csv files
n=length(l);
data=cell(1,n);
for i=1:n
data(i)=csvread(l(i).name);
end
The problem is that my csv files look like below (its actual size is bigger) and have strings in the first row so I cannot use csvread because I get the following error:
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
nodenumber, x-coordinate, y-coordinate,velocity-magnitude, x-velocity,
y-velocity, temperature\n
nodenumber x-coordinate y-coordinate velocity-magnitude x-velocity y-velocity temperature
1 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.98E+01
2 1.25E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.93E+01
3 2.50E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.87E+01
4 3.75E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.80E+01
5 5.00E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.74E+01
6 6.25E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.67E+01
7 7.50E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.60E+01
8 8.75E-04 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.54E+01
9 1.00E-03 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.47E+01
10 1.12E-03 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.40E+01
11 1.25E-03 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.34E+01
12 1.37E-03 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.27E+01
13 1.50E-03 0.00E+00 0.00E+00 0.00E+00 0.00E+00 9.21E+01
my goal is loading all csv files data do some math on the data and save them in a matrix. What do you suggest to solve this problem?
thank you in adavance!
0 commentaires
Réponse acceptée
Guillaume
le 25 Jan 2019
csvread (and dlmread) can't read the headers of csv files. You can tell them to skip the header:
data(i)=csvread(l(i).name, 1, 0); %skip 1st row
But better would be to use the modern tools of matlab and use readtable to actually parse the header and use it to name the variables (columns) of the table:
data(i) = readtable(l(i).name); %why not call the variable csvlist instead of l? It's instantaneously clearer what its purpose is?
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets 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!