Update time/date in a for loop

7 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by