如何在Matlab中绘制两年一次的数据

时间:2021-02-17 04:18:01

I have 2 data points for each year, one for the first half year, one for the second half year. How can I plot them in Matlab? For example:

我每年有2个数据点,一个是上半年,一个是下半年。如何在Matlab中绘制它们?例如:

ExampleData = [5; 1; 3; 2; 1; 5]

where ExampleData(1) is the value for the first half of 2008, ExampleData(2) is the value for the second half of 2008, ExampleData(3) for the first half of 2009, etc. Matlab datetime command only works for monthly or daily data.

其中ExampleData(1)是2008年上半年的值,ExampleData(2)是2008年下半年的值,2009年上半年是ExampleData(3),等等.Matlab datetime命令仅适用于月份或每日数据。

How can I either efficiently transform my data to make plots with axes that make sense, or, the other way round, which type of axis can I use for these biannual data?

如何有效地转换我的数据以制作具有合理意义的轴的图,或者反过来说,我可以将这种类型的轴用于这些一年两次的数据?

1 个解决方案

#1


1  

You can pick a start date, for the first data point

您可以为第一个数据点选择开始日期

startdate = datetime(2008, 1, 1); % 1st Jan 2008

Then, given your example data, create a datetime vector which has 6 month intervals and matches the length of your data

然后,根据您的示例数据,创建一个日期时间向量,该向量具有6个月的间隔并匹配数据的长度

ExampleData = [5; 1; 3; 2; 1; 5];
dates = startdate + calmonths((0:numel(ExampleData)-1)*6);
% dates = [01-Jan-2008, 01-Jul-2008, 01-Jan-2009, 01-Jul-2009, ...]

Then plotting is easy

然后绘图很容易

plot(dates, ExampleData);          % plot
set(gca, 'xtick', datenum(dates)); % Set x-axis ticks to be fixed 6-monthly

Output:

输出:

如何在Matlab中绘制两年一次的数据

#1


1  

You can pick a start date, for the first data point

您可以为第一个数据点选择开始日期

startdate = datetime(2008, 1, 1); % 1st Jan 2008

Then, given your example data, create a datetime vector which has 6 month intervals and matches the length of your data

然后,根据您的示例数据,创建一个日期时间向量,该向量具有6个月的间隔并匹配数据的长度

ExampleData = [5; 1; 3; 2; 1; 5];
dates = startdate + calmonths((0:numel(ExampleData)-1)*6);
% dates = [01-Jan-2008, 01-Jul-2008, 01-Jan-2009, 01-Jul-2009, ...]

Then plotting is easy

然后绘图很容易

plot(dates, ExampleData);          % plot
set(gca, 'xtick', datenum(dates)); % Set x-axis ticks to be fixed 6-monthly

Output:

输出:

如何在Matlab中绘制两年一次的数据