MATLAB Answers


CSV Date time error

Asked by Dan Howard on 5 Feb 2016
Latest activity Commented on by Peter Perkins
on 10 Feb 2016
Hello all,
I hope someone can help point me in the right direction. I may have been staring too much at how to achieve this but here goes:
Currently testing out how to import CSV file into matlab whiles using the date time information from the file onto a graph.
The file imports ok and shows the date time as e.g
if true
% 2013-12-16 11:21:51.110
2013-12-17 13:53:53.710
2013-12-17 10:04:58.710
This is displayed in a 65x1 cell called date_time and the other column is called amplitude which shows numerical data.
Im trying to plot a scatter graph which uses the date_time against the amplitude but keep on getting an error (Error using plot Invalid first data argument).
I have tried to convert the csv file into excel, import into matlab in where the date_time is shown in numerical values. This does work although the date time information is not displayed on the graph.
The first option would be easier to use as this will stop having to import via excel first then into matlab. Would the date_time need to be converted into numerical points first, if so im not sure how this is done.
Would appreciate some guidence on this. Sure its pretty easy but Matlab is slowly defeating me.
If you need anymore information on this happy to send some over.
Many thanks


Sign in to comment.


2 Answers

Answer by Ingrid
on 5 Feb 2016

what is your code to read the CSV file? You say that it reads in ok but there might be a better way of reading in your dates such that you automatically read the string and convert it to a datetime value that you can use to plot. This can be done if you use %{fmt}D in textscan
also, to show the string of dates instead of the numerical values have a look at
doc datetick


Sign in to comment.

Answer by Peter Perkins
on 5 Feb 2016

Dan, your description is a little light on details. Assuming you have data like a bigger version of this ...
timestamps = ...
{'2013-12-16 11:21:51.110';
'2013-12-17 13:53:53.710';
'2013-12-17 10:04:58.710';
'2013-12-18 05:23:51.710';
'2013-12-17 22:58:50.710'};
amplitude = rand(5,1);
... then this code (in R2014b or later) ...
timestamps = datetime(timestamps,'Format','yyyy-MM-dd HH:mm:ss.SSS')
will make a figure like this:
Hope this helps.


Peter, still on the same topic. quick question..
converting numerical data (ie op_datetime 1372x1 table)
if true
% op_datetime = datenum('op_datetime','dd-mmm-yyyy HH:MM:SS')
This gives an error saying:
Error using datenum (linie 178) DATENUM failed.
Caused by: Error using dtstr2dtnummx Failed on converting date string to date number.
Am I right in using datenum?
Converting numerical date that is stored in a table to the date/time format 'dd-mmm-yyyy HH:MM:SS'.
Many thanks
Forgot to include the numberical data:
if true
% 41855.5185077546
Entire table consists (1372x1 table)
It's not clear how you're reading things in or what you end up with.Let's say you have a file that looks like this:
2013-12-16 11:21:51.110,1
2013-12-17 13:53:53.710,2
2013-12-17 10:04:58.710,3
2013-12-18 05:23:51.710,4
2013-12-17 22:58:50.710,5
That's a CSV file, you've got a spreadsheet (I think), but readtable doesn't really care. This line reads it into a table:
>> t = readtable('tmp4.dat')
t =
date_time amplitude
_________________________ _________
'2013-12-16 11:21:51.110' 1
'2013-12-17 13:53:53.710' 2
'2013-12-17 10:04:58.710' 3
'2013-12-18 05:23:51.710' 4
'2013-12-17 22:58:50.710' 5
If you're using a version of MATLAB prior to R2014b, you can plot the amplitudes vs. time like this:
>> plot(datenum(t.date_time,'yyyy-mm-dd HH:MM:SS.FFF'),t.amplitude,'o')
>> datetick('x')
If you're using R2014b or later, I'd recommend this:
>> t.date_time = datetime(t.date_time,'Format','yyyy-MM-dd HH:mm:ss.SSS')
t =
date_time amplitude
_______________________ _________
2013-12-16 11:21:51.110 1
2013-12-17 13:53:53.710 2
2013-12-17 10:04:58.710 3
2013-12-18 05:23:51.710 4
2013-12-17 22:58:50.710 5
>> plot(t.date_time,t.amplitude,'o')
That will make the plot I posted earlier.
Hope this helps.

Sign in to comment.