필터 지우기
필터 지우기

Update time/date in a for loop

조회 수: 3 (최근 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 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