MATLAB Answers

Extracting specific rows with different content from a large table array

9 views (last 30 days)
Carl Schneegaß
Carl Schneegaß on 28 Sep 2020
Answered: Steven Lord on 28 Sep 2020
I have several tables (a x 3; double, double, string).
The first double contains Matlab Dates, the 2nd contains numbers between 0 and 1.
I would like to extract the data of specific days, e.g. SpecificDay-30:SpecificDay+10
For example, I would like to extract all 41 rows where MatDate is (SpecificDay-30:SpecificDay+10) when SpecificDay = 736958 (which is datenum(2017,09,20)).
This is how one of the tables looks like:
Thank you so much in advance!

  0 Comments

Sign in to comment.

Answers (4)

madhan ravi
madhan ravi on 28 Sep 2020
ix = find(TaBle.MatDate == 736958);
Wanted = TaBle{(ix-30):(ix+10), :}

  0 Comments

Sign in to comment.


Turlough Hughes
Turlough Hughes on 28 Sep 2020
Tnew = T(T.MatDate>=SpecificDay-30 & T.MatDate<=SpecificDay+10,:);

  0 Comments

Sign in to comment.


Steven Lord
Steven Lord on 28 Sep 2020
Consider storing your data in a timetable, converting your MatDate variable into a datetime array and using table2timetable (or manually creating a timetable from the variables.) If you do this you can use timerange to extract rows in a certain interval.
rng(999, 'twister') % Arbitrarily chosen to make the problem a little more interesting
% Create some sample data
N = datetime('now');
dt = N + minutes(randi([-180 180], 10, 1));
x = (1:10).';
% Build a timetable
T = timetable(dt, x)
% Make the timerange to use in indexing into T
thirtyToSixty = timerange(N+minutes(30), N+minutes(60))
% Index into T
T(thirtyToSixty, :)

  0 Comments

Sign in to comment.


Sudheer Bhimireddy
Sudheer Bhimireddy on 28 Sep 2020
Convert the datenum to datetime, and then you can use the MATLAB function isbetween() to get all the indices that satisty your criteria.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by