Adding date and time on x axes in a plot

14 ビュー (過去 30 日間)
sc
sc 2021 年 4 月 7 日
回答済み: Star Strider 2021 年 4 月 7 日
Good morning,
is it possible to create a plot on Matlab, inserting on the x-axis both the date and time coming from two different columns of a table, and on the y-axis another variable?
Thank you!

回答 (1 件)

Star Strider
Star Strider 2021 年 4 月 7 日
There are (at least) two options, depending on what the dates and time are, specifically with respect to the type of arrays they are.
Using datetime arrays:
dates = repmat(datetime([2021 04 08]),12,1);
times = datetime('00:00:00','Format','HH:mm:ss')+hours(0:11).';
datestimes = dates + timeofday(times);
figure
plot(datestimes, sin(0:numel(datestimes)-1)*2*pi/numel(datestimes))
grid
Or, using essentially the same data (for comparison) using datenum arrays:
dndates = datenum(dates);
dntimes = datenum(times);
dndatestimes = fix(dndates) + rem(dntimes,1);
figure
plot(dndatestimes, sin(0:numel(datestimes)-1)*2*pi/numel(datestimes))
grid
set(gca, 'XTick',dndatestimes)
datetick('x', 'yyyy/mm/dd HH:MM:SS', 'keepticks')
Extracting the original datetime variables from a table array is straightforward, so I left that part out.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by