Update time/date in a for loop
조회 수: 5 (최근 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!
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!