plot chart with the respective date on the x axis

Hi,
I have a data set with two columns, in the second there are daily historical data which I would like to plot. This is no problem. In the first column of the data set, there is the respective data which should be displayed on the x-axis.
The thing is that the whole data set consists of more than 5000 rows and I do not want to have displayed 5000 dates on the x-axis but just the years, as follows
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
In excel that is no big deal but I do not know how to do that in Matlab

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 24 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 24 Mär. 2013

0 Stimmen

Look at this example
dates=datenum('01/01/2000'):datenum('01/01/2009')
y=sin(0.01*dates)
plot(dates,y)
d=datevec(dates)
[a,idx]=unique(d(:,1),'first')
set(gca,'xtick',dates(idx),'xticklabel',a)

11 Kommentare

that's kind of what I am lokking fors
the thing is, that both the values as well as the dates are stores as a vector and from the vector which includes all the dates I would only like to take the year from the first date of each year and plot that in the x-axis
dates=datenum('01/02/1990'):datenum('03/05/2013')
y=VIX
plot(dates,y)
d=datevec(dates)
d=d(:,1)
[a,idx]=unique(d)
set(gca,'xtick',dates(idx),'xticklabel',a)
The vector with the historical data is called VIX and I would like that with the corresponding dates which starts at 02-Jan-1990 and last until 05-Mar-2013.
But like this, I get an error message saying: Error using plot Vectors must be the same lengths.
Error in VIXplotten (line 3) plot(dates,y)
Azzi Abdelmalek
Azzi Abdelmalek am 24 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 24 Mär. 2013
Type and show the result
size(y)
size(dates)
Locks
Locks am 24 Mär. 2013
Bearbeitet: Locks am 24 Mär. 2013
ans =
5837 1
ans =
1 8464
Error using plot Vectors must be the same lengths.
Error in VIXplotten (line 6) plot(dates,y)
weekens and nontrading days are excluded in the VIX vector, that's why there aren't the same number of elemets. how ever i have a data series with all the respective dates, how can I modify your code to displays the years of those dates in the x-axis, let's say the vector is named data
There is a problem with your data, y and dates should have the same size
you're right, I have edited my answer above in the mean time, is it possible to do that?
I tried this:
dates=VIXdate
y=VIX
plot(dates,y)
d=datevec(dates)
d=d(:,1)
[a,idx]=unique(d)
set(gca,'xtick',dates(idx),'xticklabel',a)
But I get the following error message: Error using plot Invalid first data argument
Error in VIXplotten (line 4) plot(dates,y)
I guess that's because the'VIXdate'-dataset is a vector consisting of characters and not numbers, could that be?
Type again
size(y)
size(dates)
And if dates is not double, use
dates=datenum(VIXdate)
ans =
5837 1
ans =
5837 11
I guess that's because VIXdate is not a number but the actual date
Use
dates=datenum(VIXdate)
Locks
Locks am 24 Mär. 2013
that's working, thanks!
Locks
Locks am 24 Mär. 2013
sorry, I have just seen now that there is still one problem. the date should be displayed at 1.1. of each year not at the end of a year, how can I change that?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Community Treasure Hunt

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

Start Hunting!

Translated by