How to read .csv file using textscan function ?

조회 수: 14 (최근 30일)
Vishnu Dhakad
Vishnu Dhakad 2018년 12월 12일
댓글: Stephen23 2018년 12월 18일
Hello guys,
I have a csv file which is attached.
I have use following code to read this but i got the error.
fbc = fopen('BC100118.CSV');
BC = textscan(fbc, '%.f-%.s-%.f,%.f:%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f,%.f');
fclose(fbc);
In this file some data also lost and that is not randomly.
I want to plot data with time.
So please help ...

채택된 답변

Stephen23
Stephen23 2018년 12월 12일
편집: Stephen23 2018년 12월 12일
opt = {'Delimiter',',','CollectOutput',true,'TreatAsEmpty','""'};
fmt = repmat('%f',1,50);
fmt = ['%q%q',fmt];
[fid,msg] = fopen('BC100118.CSV','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
And checking the imported data:
>> size(C{1}) % first two columns (date/time)
ans =
708 2
>> size(C{2}) % remaining columns (numeric)
ans =
708 50
>> C{1}(1:7,:) % first seven rows of date/time
ans =
'01-oct-18' '00:00'
'01-oct-18' '00:02'
'01-oct-18' '00:04'
'01-oct-18' '00:06'
'01-oct-18' '00:08'
'01-oct-18' '00:10'
'01-oct-18' '00:12'
>> C{2}(1:7,1:7) % first seven rows of first seven columns of numeric data
ans =
24601 26066 25801 26824 27751 28098 28102
24467 25951 25514 26484 27156 27622 27792
24257 25735 25431 26598 27705 28187 28310
24177 26080 25734 26721 27502 28588 28673
24019 25793 25450 26574 27462 27791 27769
23723 25349 25000 26088 26620 27356 27247
22867 24538 24292 25299 26234 26885 27176
>> C{2}(1:7,44:50) % last seven columns
ans =
42.702 0.0212 0.8107 0.0212 2.0430 1 39.452
44.606 0.0212 0.7964 0.0212 2.0420 1 41.230
46.556 0.0212 0.7826 0.0212 2.0424 1 43.046
48.523 0.0212 0.7689 0.0212 2.0428 1 44.877
50.439 0.0212 0.7553 0.0212 2.0417 1 46.653
52.325 0.0212 0.7425 0.0212 2.0413 1 48.395
54.177 0.0212 0.7303 0.0212 2.0417 1 50.132
  댓글 수: 12
Vishnu Dhakad
Vishnu Dhakad 2018년 12월 18일
Hi,
I have multiple files like BC100118.CSV, BC100218.CSV, BC100318.CSV,.............
How to make batch file and read it using above code.
Thank you

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

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 12월 12일
[num,txt,raw]=xlsread('yourcsv.csv');
plot(t,num)
  댓글 수: 1
Vishnu Dhakad
Vishnu Dhakad 2018년 12월 12일
Thanks for your suggetion
I want to use "textscan" function.
Can you help me

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by