简体   繁体   English

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

[英]Create and Plot Time Series Data in Matlab

I have a (1x700) vector x for which I would like to create and plot a time series object in Matlab. 我有一个(1x700)向量x,我想在Matlab中创建和绘制时间序列对象。 Each observation corresponds to one month, and the first observation belongs to January 1960. I tried the following: 每个观测值对应一个月,第一次观测值属于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);

However, I still see numbers on the x-axis instead of years. 但是,我仍然在x轴上看到数字,而不是年份。 Could anyone please help? 谁能帮忙吗? Thanks in advance! 提前致谢!

Create random data. 创建随机数据。 1/12 corresponds to the fraction of a year that each month represents. 1/12对应于每个月代表的一年的分数。

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

Then plot the x and y axes data using plot . 然后使用绘制x和y轴数据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. 1960:1970将分别产生[1960 1961 ...],分别对应于刻度的年份。

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

Here is the output plot. 这是输出图。

在此处输入图片说明

Doing 1 year intervals get VERY MESSY with lots of data. 每隔1年进行一次,会获得非常混乱的数据。 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. 下面的代码显示了如何设置5年间隔。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM