How to plot data with corresponding dates

I have a vector with data (1x9324 double) and another vector (1x9324 cell) with the corresponding dates. Both vectors are from an Excel sheet using 'xlsread'. The dates are in string format and not equidistant. e.g.
'02.01.2007 08:45:00'
to
'31.12.2008 12:27:00'
'31.12.2008 15:31:00'
'31.12.2008 16:35:00'
'31.12.2008 16:35:00'
'31.12.2008 16:35:00'
There are also dates wich are exactly the same. But I do not want to interpolate or take averages. I want to plot the data on the Y axis and the dates on the X axis, just month and year are enough. I tryed
startDate = datenum('02.01.2007');
endDate = datenum('31.12.2008');
xData = linspace(startDate,endDate,9324);
plot(xData,data_vector);
datetick('x','mmm,yy','keepticks');
But I'm afreid that the dates are now equidistant. Then I tryed
ts = timeseries (data_vector, datenum(date_vector));
plot(ts);
But both vectors changed in length and values. Maybe because the timeseries tool is taking averages of the data where the dates are the same.
System:
MATLAB 7.10.0 (R2010a)
Microsoft Excel 97-2003 Worksheet (.xls)
Windows 8.1 64Bit
Thanks in advance!

 採用された回答

David Young
David Young 2014 年 9 月 16 日
編集済み: David Young 2014 年 9 月 18 日

0 投票

The problem is that you are using linspace to produce equally separated dates rather than converting each of the actual dates. Try
plot(datenum(dates, 'dd.mm.yyyy HH:MM:SS'), data_vector);
where dates is the cell array of strings containing your dates.
[Answer edited to include format string for datenum]

3 件のコメント

Karl-Martin
Karl-Martin 2014 年 9 月 16 日
When I do the following
plot(datenum(dates), data_vector);
datetick('x','mmm,yy','keepticks');
I get this graph
It seems like the x values are not clearly linked to the y values and the dates look all the same. Maybe there is something wrong with the date conversion.
When I test my first date with
datenum('02.01.2007 08:45:00')
ans = 7.3560e+005
And back again with
datestr(ans)
I get a different date
ans =
01-Jan-2014 08:45:00
David Young
David Young 2014 年 9 月 17 日
Yes, you're right - the date string isn't in the standard format, so isn't being converted correctly. This should work though:
datenum(dates, 'dd.mm.yyyy HH:MM:SS')
David Young
David Young 2014 年 9 月 18 日
Note: answer edited to reflect this.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2014 年 9 月 16 日

編集済み:

2014 年 9 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by