Matlab 合并两个(或多个)带时间戳的数据,并绘制显示间隙的曲线图

Matlab 合并两个(或多个)带时间戳的数据,并绘制显示间隙的曲线图,matlab,matlab-figure,Matlab,Matlab Figure,合并并绘制2(或更多)时间戳数据的最佳方法是什么,以便绘制包含已知数据之间的间隙 例如,我有一个单元格,其中包含周一的时间和心率值,还有一个单元格包含周五的时间和心率值。我想绘制从周一到周五的所有数据,包括间隙,显示周二到周四没有记录任何数据 到目前为止,如果我合并数据 data = [mon;fri] % get the time serial numbers dateInd = 1; dateString = data(dateIndex); dateFormat = 'dd/mm/yyy

合并并绘制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');
很明显,我将周一和周五的数据合并到一个长达两天的绘图中。但我想有一个5天的绘图,数据只显示在周一和周五。实现这一目标的最佳方式是什么

我希望这是有道理的

非常感谢,


Jon

为了达到这种效果,我通常用
NaN
s填充缺失的数据,如下所示:

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没有绘制
x
y
数据为
NaN
s的绘图点。 在您的情况下,您可以将缺少的时间点添加到您的时间数据中(以获得正确的间隔),并将
NaN
s添加到相应的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-data具有完整的时间信息,包括日期(例如,如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-');
您可以这样做(绘图数量不受限制):


为了达到这种效果,我通常用
NaN
s填充缺失的数据,如下所示:

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没有绘制
x
y
数据为
NaN
s的绘图点。 在您的情况下,您可以将缺少的时间点添加到您的时间数据中(以获得正确的间隔),并将
NaN
s添加到相应的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-data具有完整的时间信息,包括日期(例如,如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-');
您可以这样做(绘图数量不受限制):


非常感谢。我正在努力在数据中插入NAN,以填充空间以获得所需的结果。但是,我不知道如何在一个时间轴上绘制两个单独的图,第二个图会像你提到的那样移动?有什么建议吗?再次感谢,乔恩,非常感谢。我正在努力在数据中插入NAN,以填充空间以获得所需的结果。但是,我不知道如何在一个时间轴上绘制两个单独的图,第二个图会像你提到的那样移动?有什么建议吗?再次感谢你,乔恩