1

合并和绘制 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 天的图,数据仅在周一和周五显示。实现这一目标的最佳方法是什么?

我希望这是有道理的,

非常感谢,

乔恩

4

1 回答 1

1

为了达到这样的效果,我通常用 s 填充缺失的数据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 不会在其中xory数据为NaNs 的位置绘制绘图点。在您的情况下,您可以将缺失的时间点添加到您的时间数据(以具有正确的间隙)并将NaNs 添加到相应的 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;
于 2013-06-13T10:39:11.163 回答