Reading a csv file containing not only numerical data
34 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
simo boni
le 27 Juin 2015
Réponse apportée : Morteza Darvish Morshedi
le 8 Juin 2020
Hi,
I have this sort of file.csv
Date Time Voltage L12 Min Voltage L12 Avg Voltage L12 Max Voltage L23 Min Voltage L23 Avg
06/04/2015 16:33:01 745ms 150.660 150.740 150.840 150.560 150.660
06/04/2015 16:33:02 245ms 150.760 150.780 150.800 150.460 150.560
06/04/2015 16:33:02 745ms 150.720 150.740 150.740 150.620 150.720
06/04/2015 16:33:03 245ms 150.620 150.680 150.740 150.580 150.680
As you see with different type of data. I only need to import the numerical part and so I am using the following command
filename = 'Trial1.csv';
M = csvread(filename,3,3)
I have tried with different number of rows an column in order to avoid non numerical data, but I always receive this error message.
Error using dlmread (line 139)
Number of HeaderColumns is greater than number of columns in file.
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
Error in EnergyReading_20150627 (line 8)
M = csvread(filename,3,3)
It looks like I have different number of columns but is actually a perfect rectangular matrix on the csv file.
Please any suggestions?
0 commentaires
Réponse acceptée
Star Strider
le 27 Juin 2015
Use the textscan function to read it instead. Without your actual file to experiment with, and not knowing exactly what columns you want to import, I can’t write a specific textscan call.
2 commentaires
Star Strider
le 27 Juin 2015
I can’t help you import your file without having at least a sample of it to work with. Please upload it. (Use the ‘paperclip’ icon to import it. Be sure to first select your file with ‘Choose file’, then click on the ‘Attach file’ button to upload it.)
Also, see if you can open it and import your data with the xlsread function. That will work if it was an Excel .csv file. The easiest way to determine that is to see if Excel will open it.
Plus de réponses (3)
Walter Roberson
le 4 Déc 2016
csvread and dlmread cannot be used for mixed strings and numbers at all in older versions and in more recent versions can only be used for mixed strings and numbers if the row and column specification is passed by the user and none of the selected input includes strings. In the user's files the 'ms' after the first number would rule out using csvread or dlmread
With more modern MATLAB versions you could try using readtable
The header line of the user's file has headers that have two words in them, like 'L12 Min'. That throws off the column counting when used with space delimited columns. You might need to use a HeaderLines option to textscan or readtable to skip the header.
readtable can read dates and times but when columns are space separated it might put them in separate columns. There are new options in R2016b for readtable that might possibly allow that to be overcome.
textscan can be used for cases like this.
0 commentaires
Morteza Darvish Morshedi
le 8 Juin 2020
I would recomment readtable with passing delimiter parameter in the csv file.
0 commentaires
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!