Reading a csv file containing not only numerical data

34 vues (au cours des 30 derniers jours)
simo boni
simo boni le 27 Juin 2015
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?

Réponse acceptée

Star Strider
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
simo boni
simo boni le 27 Juin 2015
Thanks for you answer. Let's say I need to import this:
Date Time L12Min L12Avg L12Max L23Min L23Avg L23Max
06/04/2015 16:33:01 745ms 150.660 150.740 150.840 150.560 150.660 150.740
06/04/2015 16:33:02 245ms 150.760 150.780 150.800 150.460 150.560 150.660
06/04/2015 16:33:02 745ms 150.720 150.740 150.740 150.620 150.720 150.780
06/04/2015 16:33:03 245ms 150.620 150.680 150.740 150.580 150.680 150.800
06/04/2015 16:33:03 745ms 150.700 150.800 150.900 150.520 150.700 150.840
06/04/2015 16:33:04 245ms 150.720 150.720 150.720 150.640 150.700 150.760
As I said I do not necessary need to import first row and date and time but could be nice.
Star Strider
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.

Connectez-vous pour commenter.

Plus de réponses (3)

simo boni
simo boni le 30 Juin 2015
I've solved my problem using the Matlab tool for importing. I simply ask Matlab to return me the script for the importation.
  2 commentaires
Tanvi Nagarale
Tanvi Nagarale le 4 Déc 2016
how?
Tanvi Nagarale
Tanvi Nagarale le 4 Déc 2016
I am getting the same error

Connectez-vous pour commenter.


Walter Roberson
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.

Morteza Darvish Morshedi
Morteza Darvish Morshedi le 8 Juin 2020
I would recomment readtable with passing delimiter parameter in the csv file.

Community Treasure Hunt

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

Start Hunting!

Translated by