I have time in hours from 0-72 hours (i.e. 0, 1, 2, 3,..., 72). i.e. a = 0:72;
I would like to convert this hours into datestr with 'yyyy-MM-dd hh:mm:ss' format.
I know initial yyyy-MM-dd i.e. 2012-04-24.
So how can i set my output in 'yyyy-MM-dd hh:mm:ss' format?
I want o/p to be like;
2012-04-24 00:00:00, 2012-04-24 01:00:00, ...., 2012-04-25 00:00:00, 2012-04-25 01:00:00, ...., 2012-04-26 00:00:00, 2012-04-26 01:00:00
date should change with each 24 hours.
Any help will be greatly appriciated.

 採用された回答

Stephen23
Stephen23 2022 年 7 月 15 日

0 投票

dtm = datetime(2012,4,24,'Format','yyyy-MM-dd HH:mm:ss') + hours(0:72).'
dtm = 73×1 datetime array
2012-04-24 00:00:00 2012-04-24 01:00:00 2012-04-24 02:00:00 2012-04-24 03:00:00 2012-04-24 04:00:00 2012-04-24 05:00:00 2012-04-24 06:00:00 2012-04-24 07:00:00 2012-04-24 08:00:00 2012-04-24 09:00:00 2012-04-24 10:00:00 2012-04-24 11:00:00 2012-04-24 12:00:00 2012-04-24 13:00:00 2012-04-24 14:00:00 2012-04-24 15:00:00 2012-04-24 16:00:00 2012-04-24 17:00:00 2012-04-24 18:00:00 2012-04-24 19:00:00 2012-04-24 20:00:00 2012-04-24 21:00:00 2012-04-24 22:00:00 2012-04-24 23:00:00 2012-04-25 00:00:00 2012-04-25 01:00:00 2012-04-25 02:00:00 2012-04-25 03:00:00 2012-04-25 04:00:00 2012-04-25 05:00:00

4 件のコメント

MP
MP 2022 年 7 月 15 日
編集済み: MP 2022 年 7 月 15 日
Thank you so much for your efforts.
This is datetime array. Could you please tell me how to get a string. I want to put xticklabels.
"something like datestr"
MP
MP 2022 年 7 月 15 日
Ok I got it
b = char(dtm); will get me desired output.
Thanks a lot.
Stephen23
Stephen23 2022 年 7 月 15 日
編集済み: Stephen23 2022 年 7 月 15 日
str = string(dtm) % string array
txt = cellstr(dtm) % cell array of character vectors
Steven Lord
Steven Lord 2022 年 7 月 15 日
I want to put xticklabels.
If you're plotting using this datetime data as one of the inputs in your call to plot, the tick labels will automatically be generated from the plotted datetime array. You don't need to convert to a string array in this case.
v = 0:10;
x = datetime('today') + days(v);
y = v.^2;
plot(x, y)

サインインしてコメントする。

その他の回答 (1 件)

MP
MP 2022 年 7 月 15 日

0 投票

Yes, that did a great help.... :)
Thank you so much @Stephen23

カテゴリ

ヘルプ センター および File Exchange で Dates and Time についてさらに検索

質問済み:

MP
2022 年 7 月 15 日

コメント済み:

2022 年 7 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by