Why this error shows up when I run the code and when my friend runs it it doesn't appear: Error using datenum (line 179) DATENUM failed. Error in dateroll (line 7) if isbusday(d​atenum(dat​e(i)))==0 Caused by: Error using datevec (line 277)

6 views (last 30 days)
This function is created: function f=dateroll(date)
holidays=[]; f=[];
for i=1:length(date) if isbusday(datenum(date(i)))==0 if month(busdate(date(i),1,holidays))==month(date(i)) f(i)=busdate(date(i),1,holidays); else f(i)=busdate(date(i),-1,holidays); end else f(i)=datenum(date(i)); end end
f=datestr(f);
And when I run it
there is always this error:
Error using datenum (line 179) DATENUM failed.
Error in dateroll (line 7) if isbusday(datenum(date(i)))==0
Caused by: Error using datevec (line 277) Cannot parse date 1.12.2006..
Thanks for Help

Answers (2)

dpb
dpb on 10 Jan 2014
Error in dateroll (line 7) if isbusday(datenum(date(i)))==0
Caused by: Error using datevec (line 277) Cannot parse date 1.12.2006..
Insufficient evidence for conclusive but prime possibility is using different input data although there could be more insidious differences of installed version or an aliased datenum or somesuch.
Oh, actually, I see...note the error message format -- there are two (2) periods at the end such that the input string is including a trailing dot. I think this implies your input parser isn't handling date strings missing the leading '0' in the month/day fields as in the above. And, of course, the string is ambiguous -- is it MM/DD or DD/MM order? That's actually the real reason for the failure altho the length is potentially a problem you'll need to solve as well.
Example--
>> datenum('1.12.2006.')
Error using datenum (line 179)
DATENUM failed.
Caused by:
Error using datevec (line 277)
Cannot parse date 1.12.2006..
>> datenum('1.12.2006')
Error using datenum (line 179)
DATENUM failed.
Caused by:
Error using datevec (line 277)
Cannot parse date 1.12.2006.
>> datenum('1.12.2006','MM.DD.YYYY')
ans =
7.3269e+05
NB that the trailing '.' didn't cause a problem when the format is given but you may find other data are corrupt as well in the file if you used fixed length parsing.
See
doc datenum
and note the note on the form for input you've used of
datenum(S)
that says
N = datenum(S)
converts the string or date vector (as defined by
DATEVEC) S into a serial date number. If S is a string, it must be in
one of the date formats 0,1,2,6,13,14,15,16,23 as defined by DATESTR.
This calling syntax is provided for backward compatibility, and is
significantly slower than the syntax which specifies the format string.
If the format is known, the N = datenum(S,F) syntax should be used.

Andrea
Andrea on 10 Jan 2014
Thank you for answer.
Now I have looked at the dates imported in matlab and I don't know why but after each date Matlab puts '.'
I don't know how to get rid full-stop when importing dates from excel into Matlab.
Here is the code that reads data from Excel:
if true
clear
clc
format bank
[num txt raw]=xlsread('data.xlsx','Given data','a1:l41');
D_reset=txt(2:end,1);
D_payment=txt(2:end,2);
global t
t=datenum(2007,4,5);
N_floating=num(1:end,1);
N_fixed=[num(1:8,2); zeros(32,1)];
N_collar=[zeros(8,1); num(9:end,2)];
s_float=num(1:end,3);
s_collar=num(1:end,5);
c_fixed=num(1:end,4);
c_knownfl=num(1:end,10);
max=num(1:end,6);
min=num(1:end,7);
ex=num(1:end,9);
finalex=num(1:end,8);
adjD_reset=dateroll(D_reset);
adjD_payment=dateroll(D_payment);
adjT_reset=yearfrac(t,adjD_reset,2);
adjT_payment=yearfrac(t,adjD_payment,2);
adj_tenor=adjT_payment-adjT_reset;
t=datestr(datenum(2007,04,05));
  1 Comment
dpb
dpb on 10 Jan 2014
Edited: dpb on 11 Jan 2014
Need to see a section of the returned text that you're processing to get the input strings to tell for absolute certain, but that you're using a fixed length count is the most likely culprit.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!