Add data tips at specific date time
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
Les Beckham
am 10 Dez. 2025 um 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 Kommentar
Walter Roberson
am 11 Dez. 2025 um 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');
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
