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 ...

 Respuesta aceptada

Stephen23
Stephen23 el 12 de Dic. de 2018
Editada: Stephen23 el 12 de Dic. de 2018

1 voto

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 comentarios

Vishnu Dhakad
Vishnu Dhakad el 13 de Dic. de 2018
Editada: Vishnu Dhakad el 13 de Dic. de 2018
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 el 13 de Dic. de 2018
@Vishu Dhakad: what MATLAB version are you using?
Vishnu Dhakad
Vishnu Dhakad el 13 de Dic. de 2018
R2017a
Stephen23
Stephen23 el 13 de Dic. de 2018
Editada: Stephen23 el 13 de Dic. de 2018
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 el 14 de Dic. de 2018
Editada: Vishnu Dhakad el 14 de Dic. de 2018
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 el 14 de Dic. de 2018
I want to count number of break in the plot.
can you help me?
Stephen23
Stephen23 el 14 de Dic. de 2018
@Vishu Dhakad: how are the breaks indicated in the data: NaN values?
Vishnu Dhakad
Vishnu Dhakad el 14 de Dic. de 2018
Yes, NAN Value.
Stephen23
Stephen23 el 14 de Dic. de 2018
Editada: Stephen23 el 14 de Dic. de 2018
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 el 17 de Dic. de 2018
Thank you very much
Vishnu Dhakad
Vishnu Dhakad el 18 de Dic. de 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

Iniciar sesión para comentar.

Más respuestas (1)

madhan ravi
madhan ravi el 12 de Dic. de 2018
[num,txt,raw]=xlsread('yourcsv.csv');
plot(t,num)

1 comentario

Vishnu Dhakad
Vishnu Dhakad el 12 de Dic. de 2018
Thanks for your suggetion
I want to use "textscan" function.
Can you help me

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Dic. de 2018

Comentada:

el 18 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by