Update time/date in a for loop

2 ビュー (過去 30 日間)
Yoni Verhaegen
Yoni Verhaegen 2019 年 6 月 6 日
回答済み: Steven Lord 2019 年 6 月 6 日
Hi all,
I have defined the following variables when running my program to derive weather forecast maps:
mydate = 20190606; % Current yearmonthday (8 digits)
myrun = 06; % Current hour 00 06 12 18Z (2 digits)
myforecasthour = 033:043; % Current time + number of hours ahead (3 digits)
How can I now plot the title of my figures in my loop as the date of the forecasted time period?
For example, I use the weather computer run that was issued at 20190606 06h (current mydate + myrun) and I forecast for +33Z until +43Z (my forecasthour), so the date of my figures would be "Jun 7 2019 15Z" to "Jun 8 2019 1Z"
Thanks!

採用された回答

Steven Lord
Steven Lord 2019 年 6 月 6 日
I would use datetime and string.
mydate = 20190606; % Current yearmonthday (8 digits)
myrun = 06; % Current hour 00 06 12 18Z (2 digits)
myforecasthour = 033:043;
Convert your mydate data into a datetime by telling datetime that the input is a number in the yyyymmdd format. Make sure to specify that you're using UTC. Specify the Format you want the datetime to use to display itself.
startDate = datetime(mydate, 'ConvertFrom', 'yyyymmdd', ...
'TimeZone', 'UTC', ...
'Format', 'MMM d yyyy HX')
Add in the hours given as myrun and the hours given for the start and end of the myforecasthour vector to give the start and end times of the forecast.
forecastrange = startDate + hours(myrun) + hours(myforecasthour([1 end]))
Convert each datetime to a string and combine it with "to" then use the resulting string as the title of your axes.
title(string(forecastrange(1)) + " to " + string(forecastrange(2)));

その他の回答 (0 件)

カテゴリ

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