在Matlab中创建和绘制时间序列数据

时间:2015-04-24 17:30:16

标签: matlab

我有一个(1x700)向量x,我想在Matlab中创建并绘制一个时间序列对象。每次观察对应一个月,第一次观察属于1960年1月。我尝试了以下内容:

state1 = timeseries(x,1:size(x,2));

state1.Name = 'Test';
state1.TimeInfo.Units = 'months';
state1.TimeInfo.StartDate = 'Jan-1960';     % Set start date.
state1.TimeInfo.Format = 'yy';         % Set format for display on x-axis.
state1.Time = state1.time - state1.time(1);      % Express time relative to the start date.

plot(state1);

但是,我仍然看到x轴上的数字而不是数年。有人可以帮忙吗?提前谢谢!

1 个答案:

答案 0 :(得分:4)

Create random data. 1/12 corresponds to the fraction of a year that each month represents.

x = 1960:1/12:1970;
y = rand(1,121);

Then plot the x and y axes data using plot.

plot( x, y )

Then set the tick as follows for a decade per year. 1960:1970 will generate [1960 1961 ...] each corresponding to the tick's year.

set( gca, 'XTick', 1960:1970 );

Here is the output plot.

enter image description here

Doing 1 year intervals get VERY MESSY with lots of data. So solutions include doing a larger interval or setting your ticks to display vertically instead of horizontally. This code below shows how to set 5 year intervals instead.

set( gca, 'XTick', 1960:5:2010 );