how to define specific indexes in a dataset?

조회 수: 1 (최근 30일)
aggelos
aggelos 2012년 4월 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에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by