plot a vertical line on datetime axis of a time series

32 次查看(过去 30 天)
Hi! I have 4 waveforms which i want to plot by using the subplot fonction. For each of them i want to plot a vertical line at a specific time. Here is my code to plot my waveform:
figure1=figure
for k4=1
File4=AllFiles4(k4).name;
X4=rdmseed(strcat(P4,File4)); %('AllFiles(k).name');
d9 = cat(1,X4.d);
% dd7=d7*gainfac2;
d10=d9/max(d9); %d2=d1/max(d1);
d6=bandpass(d10,[0.01 50],s_rate); %d3==bandpass(d2,[4 50],s_rate)%
ndp=length(d10); %ndp=length(d3);
t=[0:dt:(ndp-1)/s_rate];
dt2=datetime(2021,07,09,16,32,t);
% ndp2=length(d6);
subplot(4,1,1);
plot(dt2,d10) %plot(dt2,d4)
ylabel('Amplitude (counts)')
xlabel('time (hh:mn:ss)')
title('Waveform Geophone / Jul21')
% hold on
% tMark = datetime(16,32,41);
% % % plot([tMark tMark], 'color', 'r', 'LineWidth', 1);
% plot([tMark tMark], ylim, 'color', 'r', 'LineWidth', 1);
%
% hold off
end
when i uncomment the line that i wrote for the plot of this verical line, which are the following lines
hold on
tMark = datetime(16,32,41);
% % plot([tMark tMark], 'color', 'r', 'LineWidth', 1);
plot([tMark tMark], ylim, 'color', 'r', 'LineWidth', 1);
hold off
i got the this as plot:
can someone help me on this? i want to plot this red vertical line in each of the waveform (here was for the plot in subplot (4,1,1) at the time position 16:32:41.

回答(1 个)

Star Strider
Star Strider 2022-10-18
Perhaps —
dt = 1E-2; % Create Data
ndp = 1E+4; % Create Data
s_rate = 100; % Create Data
t=[0:dt:(ndp-1)/s_rate];
dt2=datetime(2021,07,09,16,32,t);
d10 = randn(size(dt2)) .* sin(2*pi*second(dt2)/10) % Create Data
d10 = 1×10000
0 0.0062 -0.0097 -0.0005 -0.0271 0.0470 -0.0070 -0.0686 0.0394 0.0231 -0.0156 0.1088 0.0669 -0.1076 -0.1584 -0.0003 -0.0709 -0.0595 0.0506 0.2162 0.1106 0.2599 -0.0571 0.1223 0.2426 -0.1198 0.0553 0.0449 -0.1739 0.0711
% ndp2=length(d6);
subplot(4,1,1);
hold on
plot(dt2,d10) %plot(dt2,d4)
ylabel('Amplitude (counts)')
xlabel('time (hh:mn:ss)')
title('Waveform Geophone / Jul21')
tMark = datetime(2021,07,09,16,32,41);
plot([tMark tMark], ylim, 'color', 'r', 'LineWidth', 1);
hold off
You need to define the date as well as the time in ‘tMark’.
To plot it, use the hold function. Another option is xline.
.
  4 个评论
Star Strider
Star Strider 2023-3-22
Thank you!
At the top of the Answer window on the right there are a number of options: Vote, Link, etc. Clicking on Vote should work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by