How to read .csv file using textscan function ?

25 ビュー (過去 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 Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by