如何在MATLAB中将日期整数数组转换为`datenum`objects?

时间:2017-03-15 05:54:35

标签: matlab matplotlib matlab-figure

我有一堆像这样的日期整数:

dates_array = [20060828 ,20060831 , 20060901];

并且,我想将它们用作绘图的x轴。所以,我试着像这样转换它们:

datecells = [];
for datecell=dates_array
    display(datecell)
    dn = datenum(char(datecell), 'YYYYmmdd');
    datecells = [datecells, dn];
end

我从this了解到这些dn应该是天数。没关系。但是,当我打印出datecells变量时,我得到了什么,而且我不知道为什么:

>> datecells

datecells =

      736696      736696      736696

为什么所有这三个元素都显示相同的天数?

2 个答案:

答案 0 :(得分:0)

空单元格数组的定义如下:

datecells={};

现在,将每个元素转换为格式并将其添加到单元格的末尾。

formatOut = 'dd mmm yyyy';
for i=1:3
     dt = datestr(datenum(char(dateArray[i]), 'YYYYmmdd'),formatOut);
     datecells(end+1)= dt
end

我没有测试过代码。

答案 1 :(得分:0)

在修改datenum以提取年/月/日组件后,您可以使用三参数dates_array语法。

% use a combination of MOD and floored division to extract
% year-month-day components
ymd = floor(bsxfun(@rdivide, bsxfun(@mod, dates_array, ...
    [1e8; 1e4; 1e2]), [1e4; 1e2; 1]));
% ymd is now a matrix where the 1st row is year, 2nd row is
% month, and 3rd row is day of month
dates = datenum(ymd(1,:), ymd(2,:), ymd(3,:));