Add data tips at specific date time

조회 수: 20 (최근 30일)
Lauren
Lauren 2025년 12월 10일 22:20
댓글: Walter Roberson 2025년 12월 11일 1:05
I am trying to add datatips at a specific time in my data
a is datetime
b is temperature
I would like to add datatip on b at nearest YYYY-MM-DD HH:MM using datatip().
Thanks in advance.

답변 (1개)

Les Beckham
Les Beckham 2025년 12월 10일 23:04
It's always useful to provide some sample data for questions like this. I'll make up some data for now.
Hopefully this example will help will show you what to do to get what you want with your actual data.
a = datetime('now') + duration(0, [0:120], 0, 0); % Make up some test data
b = linspace(0, 10, numel(a)) + rand(1, numel(a));
hl = plot(a,b);
grid on
xlabel 'Time'
ylabel 'Temperature'
% pick a time point an hour from now for testing - replace with your
% desired time
desired_time = datetime('now') + hours(1);
temp_at_desired_time = b(find(a>desired_time, 1));
datatip(hl, desired_time, temp_at_desired_time);
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 12월 11일 1:05
temp_at_desired_time = b(find(a>desired_time, 1));
There is another way to achieve this, that is slightly longer but is good in the case of finding the right location for several times
temp_at_desired_time = interp1(a, b, desired_time, 'previous');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by