how to define specific indexes in a dataset?

2 views (last 30 days)
My aim is to define indexes for an in sample and an out of sample test of model fitting.
% define first and last date for calibration
cal_beg_date = '01-01-2010';
cal_end_date = '31-12-2010';
% define first and last date for out-of-sample forecast
for_beg_date = '01-01-2011';
for_end_date = '31-12-2011';
D = ds.Date(:,1);
% define respective indices and period lengths
cal_beg_i = find(D==eval(cal_beg_date));
cal_end_i = find(D==eval(cal_end_date));
for_beg_i = find(D==eval(for_beg_date));
for_end_i = find(D==eval(for_end_date));
cal_period = 1+cal_end_i-cal_beg_i;
for_period = 1+for_end_i-for_beg_i;
I use dataset to create my data in matlab and the adove code returns me an error. Could anyone tell me why a get an error. thanks in advance..

Accepted Answer

Peter Perkins
Peter Perkins on 30 Apr 2012
aggelos, I think what you need is something like the datenum function, and not eval. As Per points out, eval'ing a date string is going to treat dashes as subtraction. You might create a new variable in your array,
ds.DateNum = datenum(ds.Date)
if you wanted to keep that around permanently, and then do something like
cal_beg_date = datenum('01-01-2010');
cal_end_date = datenum('31-12-2010');
calData = ds(cal_beg_date <= ds.DateNum & ds.DateNum <= cal_end_date,:)
Don't know what you're doing specifically, so the above may not be exactly what you want, but it should help.

More Answers (1)

per isakson
per isakson on 30 Apr 2012
I guess this is not what you want?
>> eval('31-12-2010')
ans =
-1991

Categories

Find more on Cell Arrays 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!