2013-06-13 135 views
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天長的情節,數據只在週一和週五顯示。達到此目的的最佳方法是什麼?

我希望這是有道理的,

非常感謝,

喬恩

回答

1

爲了達到這樣的效果,我通常填充缺失的數據與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沒有繪製任何xy數據爲的繪圖點s。 在您的情況下,您可能會將缺少的時間點添加到您的時間數據(具有corect間隙)並將NaN s添加到相應的Y值。

順便說一下,爲什麼不用兩個單獨的地塊繪製第二個X數據正確移位?

EDIT

情況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; 
+0

非常感謝。我正在努力在數據中插入NaN以填充空間以獲得期望的結果。但是,我不知道如何在一個時間軸上繪製2個獨立的繪圖,第二個繪圖像您提到的那樣移動?任何指針?再次感謝,Jon – shoo

+0

我更新了我的答案。查看編輯。 – anandr

相關問題