Add data tips at specific date time

20 views (last 30 days)
Lauren
Lauren on 10 Dec 2025 at 22:20
Commented: Walter Roberson on 11 Dec 2025 at 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.

Answers (1)

Les Beckham
Les Beckham on 10 Dec 2025 at 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 Comment
Walter Roberson
Walter Roberson on 11 Dec 2025 at 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');

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!