how to define specific indexes in a dataset?

2 次查看(过去 30 天)
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..

采纳的回答

Peter Perkins
Peter Perkins 2012-4-30
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.

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by