how to select data in rows based on date in column

Analysing climatic data for which Ihave to select certain (period) colums staring from the particular date in excel. Dealing with 3000 rows and 365 columns. Please help me

2 Comments

1) Ensure the relevant column is in datetime dataset
%Ensure the PredictionTable.Time is in datatime datatype.
2) then you could
% get Year 2016 data out for training
tr = timerange('2015-01-01' , '2015-12-31');
PredictionTable_2015 = PredictionTable(PredictionTable.Time(tr),:);
Just to be clear: Kevin is suggesting you use a timetable, and then use timerange to do the subscripting. It's the right suggestion.

Sign in to comment.

Answers (1)

Hi deepika, Thank you for your question. I agree with Kevin & Peter's comments. Assuming the dates identify the rows, first make sure the date values are stored as datetime. If your data is a timetable, use a timerange to select the desired period:
% PredictionTable is a timetable with dates as its row times
t = timerange(datetime(2015,1,1), datetime(2015,12,31), "closed");
data2015 = PredictionTable(t,:);
If the dates are stored in a variable in an ordinary table, use logical indexing instead:
d = datetime(PredictionTable.Date);
idx = d >= datetime(2015,1,1) & d <= datetime(2015,12,31);
data2015 = PredictionTable(idx,:);
If the dates are column headers rather than row data, the approach is different: convert the variable names to datetime and use them to select columns. “Selecting rows” and “selecting columns” are different operations, so the table layout and a small example would be needed for more specific code.

Categories

Asked:

on 17 Jan 2019

Answered:

on 3 Sep 2026 at 13:58

Community Treasure Hunt

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

Start Hunting!