MATLAB:带有日期时间图的copyobj

时间:2018-03-07 09:39:16

标签: matlab datetime copy

我正在尝试使用copyobj复制一个情节。一旦我合并datetime,我就很难复制一个情节。

这是我的代码:

data = randn([100,1]);
time = datetime(737126-99:737126, 'ConvertFrom', 'datenum', 'Format','defaultdate');

oldFig = figure;

hold on
barOriginal(1) = bar(data .* (data>=0), 'blue');
barOriginal(2) = bar(data .* (data<0), 'red');
hold off

oldFigTime = figure;

hold on
barOriginalTime(1) = bar(time, data .* (data>=0), 'blue');
barOriginalTime(2) = bar(time, data .* (data<0), 'red');
hold off

newFig = figure;

sub1 = subplot(2,1,1);
sub2 = subplot(2,1,2);
copyobj(barOriginal, sub1)          % working
copyobj(barOriginalTime, sub2)      % failing

1 个答案:

答案 0 :(得分:1)

如果这对您不起作用,请发表评论,但如果可以将time保留为datenum,则它对指定的示例有效:

data = randn([100,1]);
%time = datetime(737126-99:737126, 'ConvertFrom', 'datenum', 'Format','defaultdate');
time = 737126-99:737126; %just as number


oldFig = figure;

hold on
barOriginal(1) = bar(data .* (data>=0), 'blue');
barOriginal(2) = bar(data .* (data<0), 'red');
hold off

oldFigTime = figure;

hold on
barOriginalTime(1) = bar(time, data .* (data>=0), 'blue');
barOriginalTime(2) = bar(time, data .* (data<0), 'red');
datetick('x') %and now turn it into a Datetime
hold off

newFig = figure;

sub1 = subplot(2,1,1);
sub2 = subplot(2,1,2);
copyobj(barOriginal, sub1)          % working
copyobj(barOriginalTime, sub2)      % will be copied from as number
datetick('x')                       % and has to be converted too