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 Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by