Error while extracting data..... trying to parse data in order to make a big data set..

3 次查看(过去 30 天)
Hi i am trying to read data of multiple csv files that contains numeric,text data which is delimited by commas...
format of the raw data in these csv.files is shown below.
16744C9796,96440457110,08/11/11,10:50:40,08/11/11,11:19:32,28.867,1
136CC15E35,83428990517,08/11/11,11:05:37,08/11/11,11:32:00,26.383,1
F40B93397476,268330551833718,08/11/11,11:05:48,08/11/11,11:32:03,26.250,1
21FEAF3F48,146006818632,08/11/11,11:08:53,08/11/11,11:33:53,25.000,1
405FBEF422BE,70779969741502,08/11/11,11:10:15,08/11/11,11:13:48, 3.550,0
A0960EACF9,689712311545,08/11/11,11:10:42,08/11/11,11:14:01, 3.308,0
1474118A3811,22488743032849,08/11/11,11:11:07,08/11/11,11:13:55, 2.800,0
I tried to use the following code but i am getting this error.
d = dir('*.csv');
N_File = numel(d);
C = {};
for num = 1:N_File
k = fullfile(d(num).name);
dat = myfileread(d(num).name,num);
C = [C;dat];
num=num+1;
end
??? Error using ==> datenum at 182
DATENUM failed.
Error in ==> myfileread at 8
t1 = datenum(strrep(strcat(data{3},'-',data{4}),'-',' '));
Caused by:
Error using ==> datevec at 286
Cannot parse date .
*
------------ The code for the above function myfileread is-----------*
function outdata = myfileread(fname,k)
% read from file
fid = fopen(fname);
data = textscan(fid,'%s%f%s%s%s%s%f%f','delimiter',',');
fclose(fid);
% extract time stamps and convert to serial date numbers
t1 = datenum(strrep(strcat(data{3},'-',data{4}),'-',' '));
t2 = datenum(strrep(strcat(data{5},'-',data{6}),'-',' '));
% combine together into one big cell array
outdata = num2cell(k*ones(size(data{1}))),data{1},num2cell([data{2},t1,t2,data{7},data{8}])];
Thanks in advance.

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-12-15
Check for a problematic .csv file. If using the .csv file you shown, it works fine.
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by