How to read .csv file using textscan function ?

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 日

1 投票

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 月 13 日
編集済み: Vishnu Dhakad 2018 年 12 月 13 日
Thanks for the information.
I have tried to plot time and numeric data but time is char type and numeric column is double type. So i got error.
Can you tell me how to plot them ??
How to convert time char into double ??
Stephen23
Stephen23 2018 年 12 月 13 日
@Vishu Dhakad: what MATLAB version are you using?
Vishnu Dhakad
Vishnu Dhakad 2018 年 12 月 13 日
R2017a
Stephen23
Stephen23 2018 年 12 月 13 日
編集済み: Stephen23 2018 年 12 月 13 日
Then you can use the datetime format %D to create date-time arrays. Read the textscan help, and read the table row "Dates and time". You will probably need something like this (untested):
fmt = repmat('%f',1,50);
fmt = ['%{dd-MMM-yy}D%{HH:mm}D',fmt];
and then manipulate the datetime objects.. These you should read too:
Vishnu Dhakad
Vishnu Dhakad 2018 年 12 月 14 日
編集済み: Vishnu Dhakad 2018 年 12 月 14 日
Thank you for reply
But I have used following code for the plot.
thank you again..
%% saprate time and date %%
dt = C{:,1};
Date = dt(:,1);
Time = dt(:,2);
%% plot data with time %%
Q = datetime(Time,'Format','HH:mm');
plot(Q, BC880,'DatetimeTickFormat','HH:mm','LineWidth',4)
Vishnu Dhakad
Vishnu Dhakad 2018 年 12 月 14 日
I want to count number of break in the plot.
can you help me?
Stephen23
Stephen23 2018 年 12 月 14 日
@Vishu Dhakad: how are the breaks indicated in the data: NaN values?
Vishnu Dhakad
Vishnu Dhakad 2018 年 12 月 14 日
Yes, NAN Value.
Stephen23
Stephen23 2018 年 12 月 14 日
編集済み: Stephen23 2018 年 12 月 14 日
isnan
diff
then count the negative values:
>> V = [1,2,NaN,NaN,3,4,NaN,NaN,5,6,7,NaN,9];
>> nnz(diff([0,isnan(V),0])<0)
ans = 3
Vishnu Dhakad
Vishnu Dhakad 2018 年 12 月 17 日
Thank you very much
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 日

0 投票

[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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

タグ

質問済み:

2018 年 12 月 12 日

コメント済み:

2018 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by