Plot datetime with data from .csv

1 view (last 30 days)
Beñat Estala
Beñat Estala on 9 Nov 2018
Commented: Peter Perkins on 15 Nov 2018
Hi, I want to plot data with dates, dates in x array and data un y arrays, but I'm having problems with the next code:
clc
clear
close all
filename='prueba.csv';
data = importfileCSVprueba(filename);
Tiempo = datetime(data(2:end,1),'InputFormat','yyyy-MM-dd HH:mm:ss.S');
Valor = data(2:end,2);
figure
plot(Tiempo,Valor, '-o')
grid
xlabel('Tiempo')
ylabel('Valor')
Thanks.
  2 Comments
Yash Ubale
Yash Ubale on 12 Nov 2018
Firstly, there is no function called 'importfileCSVprueba', use 'csvread' instead.
Also, it seems like the data in the first column of the 'prueba.csv' file is not consistent.
To read more about reading data from '.csv' files, follow the link mentioned below.
Peter Perkins
Peter Perkins on 15 Nov 2018
I recommend NOT starting out with csvread. readtable is a much better starting point in the long run. Among other things, in recent versions of MATLAB, it will in many cases create datetimes for you automatically.
In your case, the timestamps are in a bit of an unusual format, three formats in fact -- there are trailing Z's in the last third of them, and embedded T's in the last quarter of them. So:
1) clean up the file, and specify a format to readtable (or better yet, use detectimportoptions), or
2) read the timestamps as text, and convert them to datetimes in three steps, with three different formats.

Sign in to comment.

Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!