合并两个(或更多)时间戳数据和显示间隙的图表

时间:2013-06-13 09:57:03

标签: matlab matlab-figure

合并和绘制2个(或更多)时间戳数据的最佳方法是什么,以便绘图包含已知数据之间的间隙?

例如,我有一个时间和心率值为星期一的单元格,另一个为星期五。我想绘制从星期一到星期五的所有数据,其中包括从星期二到星期四没有记录的空白?

到目前为止,如果我合并数据

data = [mon;fri]

% get the time serial numbers
dateInd = 1;
dateString = data(dateIndex);
dateFormat = 'dd/mm/yyyy HH:MM:SS';
tNum = datenum(dateString{1},dateFormat);
t = linspace(tNum(1),tNum(end),length(tNum));
% get heart rates,
HRIndex = 2;
HR = data(HRIndex);

和情节

plot(t,HR)
datetick('x','ddd');

我显然将星期一和星期五的数据合并为一个2天的情节。但我希望有一个5天的情节,数据仅在星期一和星期五显示。实现这一目标的最佳方法是什么?

我希望这是有道理的,

非常感谢,

乔恩

1 个答案:

答案 0 :(得分:1)

为了达到这样的效果,我通常使用NaN来填充缺失数据,例如:

x = linspace(0,2*pi,100);
y = sin(x);
y(20:30) = NaN; % there will be a gap from point#20 to point#30
plot(x,y);

原因是MatLab不绘制xy数据为NaN s的绘图点。 在您的情况下,您可以为时间数据添加缺失的时间点(具有核心缺口),并将NaN添加到相应的Y值。

顺便说一下,为什么不绘制两个单独的图,第二个图的X数据正确移位?

修改

案例1:您的x数据是相对于一天开始的时间(0-24区间)。如果直接绘制它们,它们将重叠。您必须手动添加一些偏移量,如下所示:

% generate test data
x1 = linspace(0,1,25);     % 25 points per first day
y1 = rand(25,1);
x2 = linspace(0,1,25);     % 25 points per second day
y2 = rand(25,1);

% plot them as two separate plots
% so line style, color, markers may be set separately
XOffset = 3;
figure;
plot(x1,y1,'*k-', x2+XOffset,y2,'*r-');
% plot them as single separate plot
% so line style, color, markers are the same
figure;
plot([x1(:); NaN; x2(:)+XOffset],[y1(:); NaN; y2(:)],'*k-');
% One NaN is enough to insert a gap.

案例2:您的x数据包含完整时间信息,包括日期(例如MatLab的序列日期编号,请参阅now函数的帮助)。然后只绘制它们,它们将自动偏移。

% generate test data
XOffset = 3;
x1 = linspace(0,1,25);         % 25 points per first day
y1 = rand(25,1);
x2 = linspace(0,1,25)+XOffset; % 25 points per second day, with offset
y2 = rand(25,1);

% plot them as two separate plots
% so line style, color, markers may be set separately
figure;
plot(x1,y1,'*k-', x2,y2,'*r-');
% plot them as single separate plot
% so line style, color, markers are the same
figure;
plot([x1(:); NaN; x2(:)],[y1(:); NaN; y2(:)],'*k-');
% One NaN is enough to insert a gap.

也代替

plot(x1,y1,'*k-', x2,y2,'*r-');

你可能会这样做(图的数量不受限制):

hold on;
plot(x1,y1,'*k-');
plot(x2,y2,'*r-');
hold off;