How to read .csv file using textscan function ?

19 views (last 30 days)
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 ...

Accepted Answer

Stephen23
Stephen23 on 12 Dec 2018
Edited: Stephen23 on 12 Dec 2018
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 Comments
Vishnu Dhakad
Vishnu Dhakad on 18 Dec 2018
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

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 12 Dec 2018
[num,txt,raw]=xlsread('yourcsv.csv');
plot(t,num)
  1 Comment
Vishnu Dhakad
Vishnu Dhakad on 12 Dec 2018
Thanks for your suggetion
I want to use "textscan" function.
Can you help me

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!