How to use DATE Excel Function in Matlab vector calculations?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi all. New around here. I have been using excel date function con get dates for a insurance policy. This is my formula and would like it to translate cleanly to matlab syntax.
Excel formula: =MIN(E;MAX(B;DATE(YEAR(T)-IF(MONTH(B)<MONTH(T)+1;0;1);MONTH(B);DAY(B))))
Where:
E: Expiring date.
B: Begining date.
T: Today
I would like to apply the code to a complete table. I´ve been thinking on doing it in separate columns, but I am sure there a more efficient approach...
Thanks!
Edson.
댓글 수: 0
채택된 답변
Steven Lord
2021년 5월 2일
Don't work with serial date numbers unless you absolutely have to. Use a datetime array instead.
dt = datetime('now') + hours(randi([-5, 5], 1, 3))
min(dt)
minutes(dt(3)-dt(1))
댓글 수: 2
Steven Lord
2021년 5월 4일
There's no need to use serial date numbers.
inputFormat = 'ddMMyyyy'; % For datetime mm is minute and MM is month
T = datetime('31122016', 'InputFormat', inputFormat)
B = datetime('01102016', 'InputFormat', inputFormat)
E = datetime('30102020', 'InputFormat', inputFormat)
There are functions for extracting the parts of a datetime array:
[Y, M, D] = ymd(T)
You can use those parts in reassembling a datetime. Or you could just perform date arithmetic using a datetime and either a duration or calendarDuration array.
Tplus1_version1 = datetime(Y, M+1, D)
Tplus1_version2 = T + calmonths(1)
You can do some other calculations.
[value, location] = min([T, B, E])
Since location was 2, the second element of the datetime vector [T, B, E] contains the minimum.
추가 답변 (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!