Reading a csv file containing not only numerical data

조회 수: 32 (최근 30일)
simo boni
simo boni 2015년 6월 27일
답변: Morteza Darvish Morshedi 2020년 6월 8일
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?

채택된 답변

Star Strider
Star Strider 2015년 6월 27일
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
simo boni
simo boni 2015년 6월 27일
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 2015년 6월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

simo boni
simo boni 2015년 6월 30일
I've solved my problem using the Matlab tool for importing. I simply ask Matlab to return me the script for the importation.

Walter Roberson
Walter Roberson 2016년 12월 4일
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 2020년 6월 8일
I would recomment readtable with passing delimiter parameter in the csv file.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by