Get tenor between two time points

In excel, if some cells are in the format of date, then the difference of the cells gives you the tenor. How can I do that in Matlab?
To be specific I mean I already have some serial data number, say 20110610 and 20110716 (today's date and the expiration date of the contract), and I wanna get the number of days between these two dates. So in this example I'd like to get 36.
If I do: 20110716-20110610, the ans is 106, the same as
datenum(20110716)-datenum(20110610)=106
So my question is, again, how can I get 36 out of these two dates?
Thanks in advance :)

 채택된 답변

Andrew Newell
Andrew Newell 2011년 6월 10일

1 개 추천

d1 = datenum('20110610','yyyymmdd');
d2 = datenum('20110716','yyyymmdd');
d2-d1

댓글 수: 8

Zoe
Zoe 2011년 6월 10일
Here it is, thx :)
Zoe
Zoe 2011년 6월 10일
And actually d2 is a vector of maturity dates for me and d1 is only one entry (today's date). Is there any way to implement the same thing without using loop? ( Do you think there is a way to convert a vector using datenum as you did but without any loop?)
Fangjun Jiang
Fangjun Jiang 2011년 6월 10일
It works. What is your original data format?
datenum({'20110711','20110712'},'yyyymmdd')
Zoe
Zoe 2011년 6월 10일
%f
It is a vector of numerical numbers which was imported from .txt file.
Andrew Newell
Andrew Newell 2011년 6월 10일
You can use
d1 = floor(now);
Zoe
Zoe 2011년 6월 10일
Another question bothering me is:
d1 = datenum('20110609','yyyymmdd');
d1 =
734663
D = 20110609;
d2 = datenum('D','yyyymmdd')
d2 =
734504
Do you know why? Thx!!!
Andrew Newell
Andrew Newell 2011년 6월 10일
'D' is not the string version of the number:
>> D = 20110609;
>> D
D =
20110609
>> 'D'
ans =
D
Use d2 = datenum(num2str(D),'yyyymmdd') instead.
Fangjun Jiang
Fangjun Jiang 2011년 6월 10일
You need to learn a little bit about the data type of Matlab.
In "D=20110609", D is the variable name, its value is 20110609. In "d2=datenum('D','yyyymmdd'), D is the capital letter D. What you really need is datenum(num2str(D),'yyyymmdd').
To make it work for vector,
a=[20110711 20110712 20110712];
datenum(arrayfun(@num2str,a,'un',0),'yyyymmdd')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

질문:

Zoe
2011년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by