Update time/date in a for loop

2 vues (au cours des 30 derniers jours)
Yoni Verhaegen
Yoni Verhaegen le 6 Juin 2019
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!

Réponse acceptée

Steven Lord
Steven Lord le 6 Juin 2019
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)));

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by