Arrayfun with a timetable

3 views (last 30 days)
Allie
Allie on 10 Nov 2020
Answered: Peter Perkins on 19 Nov 2020
I need to extract items from a timetable (timetable1) matching specific dates (time2).
timetable1=[
'01-Jan-1983' -2.374
'01-Feb-1983' -2.290
'01-Mar-1983' -1.723
'01-Apr-1983' -2.117
'01-May-1983' -2.775
'01-Jun-1983' -2.913
'01-Jul-1983' -2.492
'01-Aug-1983' -2.256
'01-Sep-1983' -1.739
'01-Oct-1983' -1.108
'01-Nov-1983' -0.675
'01-Dec-1983' -0.602]
time2=[
'01-Jan-1983'
'01-Feb-1983'
'01-Mar-1983'
'01-Apr-1983'
'01-May-1983'
'01-Jun-1983'
'01-Jul-1983']
for j=1:12
timetable2(j,:)=arrayfun(@(x) timetable1.Var1(timetable1.time==x,j),time2,'UniformOutput','false')
end
Normally I would use the arrayfun code above for this type of function but when I try it with timetable data, it produces this error:
Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 81 in dimension 1. Input #3 has size 1
Is there a way to extract timetable data in this manner?

Accepted Answer

Peter Perkins
Peter Perkins on 19 Nov 2020
timetable1 is not a timetable, and probably not even legal MATLAB code. I'm going to assume you mean that you have an actual timetable with datetime row times and one numeric variable. And a list of datetimes at which you want to select rows of your timetable.
Just subscript the timetable with the datetimes. That's it.
>> tt = timetable([1;2;3;4;5],'RowTimes',datetime(2020,11,18:22))
tt =
5×1 timetable
Time Var1
___________ ____
18-Nov-2020 1
19-Nov-2020 2
20-Nov-2020 3
21-Nov-2020 4
22-Nov-2020 5
>> tt(datetime(2020,11,[18 20 22]),:)
ans =
3×1 timetable
Time Var1
___________ ____
18-Nov-2020 1
20-Nov-2020 3
22-Nov-2020 5
You should also take a look at timerange, which is a way to create a subscript that will select rows in a range rather tha exact matches.

More Answers (0)

Categories

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