CSV Date time error

4 ビュー (過去 30 日間)
Dan Howard
Dan Howard 2016 年 2 月 5 日
コメント済み: Peter Perkins 2016 年 2 月 10 日
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
end
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

回答 (2 件)

Ingrid
Ingrid 2016 年 2 月 5 日
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

Peter Perkins
Peter Perkins 2016 年 2 月 5 日
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')
plot(timestamps,amplitude,'o')
will make a figure like this:
Hope this helps.
  6 件のコメント
Dan Howard
Dan Howard 2016 年 2 月 10 日
編集済み: Dan Howard 2016 年 2 月 10 日
Forgot to include the numberical data:
if true
% 41855.5185077546
41855.5185077546
41855.5185077546
41856.423928125
41856.423928125
411856.59713622
end
Entire table consists (1372x1 table)
Peter Perkins
Peter Perkins 2016 年 2 月 10 日
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:
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
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.

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by