timeseries2timetable
Description
TT = timeseries2timetable(
converts
ts
)timeseries
objects to a timetable.
If
ts
is atimeseries
object, thenTT
is a timetable with one variable.If
ts
is an array oftimeseries
objects, thenTT
is a timetable with as many variables as there aretimeseries
objects ints
. All of thetimeseries
objects ints
must have the same sample times.If any
timeseries
object has events, then the function converts the events to an event table and attaches the event table toTT
. Duplicate events result in duplicate rows in the event table. (since R2024b)
Examples
Convert timeseries
Object to Timetable
Create a timeseries
object that has five random numbers, sampled at 10-second intervals.
ts = timeseries(rand(5,1),[0 10 20 30 40])
timeseries Common Properties: Name: 'unnamed' Time: [5x1 double] TimeInfo: tsdata.timemetadata Data: [5x1 double] DataInfo: tsdata.datametadata
Display the times and data in ts
.
ts.Time
ans = 5×1
0
10
20
30
40
ts.Data
ans = 5×1
0.8147
0.9058
0.1270
0.9134
0.6324
Convert ts
to a timetable.
TT = timeseries2timetable(ts)
TT=5×1 timetable
Time Data
______ _______
0 sec 0.81472
10 sec 0.90579
20 sec 0.12699
30 sec 0.91338
40 sec 0.63236
Timetable from Array of timeseries
Objects
Create an array of timeseries
objects. Use the same vector of sample times, but give the time series different names. Create different arrays of data values by using the rand
function.
ts1 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_1"); ts2 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_2"); ts3 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_3"); ts = [ts1 ts2 ts3]
1x3 timeseries array with properties: Events Name UserData Data DataInfo Time TimeInfo Quality QualityInfo IsTimeFirst TreatNaNasMissing Length
Combine the data from all the timeseries
objects into one timetable. Each time series in the array contributes a variable to the timetable.
TT = timeseries2timetable(ts)
TT=5×3 timetable
Time Series_1 Series_2 Series_3
______ ________ ________ ________
0 sec 0.81472 0.09754 0.15761
10 sec 0.90579 0.2785 0.97059
20 sec 0.12699 0.54688 0.95717
30 sec 0.91338 0.95751 0.48538
40 sec 0.63236 0.96489 0.80028
Input Multiple Time Series
Convert multiple timeseries
objects to a timetable.
ts1 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_1"); ts2 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_2"); ts3 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_3"); TT = timeseries2timetable(ts1,ts2,ts3)
TT=5×3 timetable
Time Series_1 Series_2 Series_3
______ ________ ________ ________
0 sec 0.81472 0.09754 0.15761
10 sec 0.90579 0.2785 0.97059
20 sec 0.12699 0.54688 0.95717
30 sec 0.91338 0.95751 0.48538
40 sec 0.63236 0.96489 0.80028
Convert timeseries
Objects That Have Events
Load three timeseries
objects that have vehicle traffic data for three city intersections.
load trafficCounts.mat count1 count2 count3
Display the properties of the first timeseries
object. The properties store the traffic data, the times at which the data was collected, and two events. The events are tsdata.event
objects.
count1
timeseries Common Properties: Name: 'Intersection1' Time: [15x1 double] TimeInfo: tsdata.timemetadata Data: [15x1 double] DataInfo: tsdata.datametadata Events: [1x2 tsdata.event]
Display the first event attached to count1
.
count1.Events(1)
EventData: [] Name: 'AMCommute' Time: 8 Units: 'hours' StartDate: ''
Display the second event. The events label the times of the morning and evening commutes.
count1.Events(2)
EventData: [] Name: 'PMCommute' Time: 18 Units: 'hours' StartDate: ''
Plot the data from the three timeseries
objects. To create a tiled chart layout for displaying the time series in subplots, use the tiledlayout
function. The chart displays red dots to mark the events associated with the three time series.
tiledlayout(3,1) nexttile plot(count1) nexttile plot(count2) nexttile plot(count3)
Convert the three timeseries
objects to one timetable. Set the format of the row times to the hh:mm
format. The events happen at the same times in the three time series, so the timetable has three event labels at both 08:00
and 18:00
.
TT = timeseries2timetable(count1,count2,count3);
TT.Time.Format = "hh:mm"
TT=15×3 timetable
Time Intersection1 Intersection2 Intersection3
_____ _____________ _____________ _____________
06:00 38 46 76
07:00 61 132 186
<3 events> 08:00 75 135 180
09:00 38 88 115
10:00 28 36 55
11:00 12 12 14
12:00 18 27 30
13:00 18 19 29
14:00 17 15 18
15:00 19 36 48
16:00 32 47 10
17:00 42 65 92
<3 events> 18:00 57 66 151
19:00 44 55 90
20:00 114 145 257
The conversion also converts the event objects to an event table. The event table is attached to the timetable. The event table shows that it has three identical events at two times.
TT.Properties.Events
ans = 6x1 eventtable
Event Labels Variable: EventLabels
Event Lengths Variable: <instantaneous>
Time EventLabels
_____ ___________
8 hr "AMCommute"
8 hr "AMCommute"
8 hr "AMCommute"
18 hr "PMCommute"
18 hr "PMCommute"
18 hr "PMCommute"
You can clean an event table that has repeated events. One approach is to use the unique
function.
ET = TT.Properties.Events; ET = unique(ET)
ET = 2x1 eventtable
Event Labels Variable: EventLabels
Event Lengths Variable: <instantaneous>
Time EventLabels
_____ ___________
8 hr "AMCommute"
18 hr "PMCommute"
Attach the cleaned event table to the timetable. Display the timetable.
TT.Properties.Events = ET
TT=15×3 timetable
Time Intersection1 Intersection2 Intersection3
_____ _____________ _____________ _____________
06:00 38 46 76
07:00 61 132 186
AMCommute 08:00 75 135 180
09:00 38 88 115
10:00 28 36 55
11:00 12 12 14
12:00 18 27 30
13:00 18 19 29
14:00 17 15 18
15:00 19 36 48
16:00 32 47 10
17:00 42 65 92
PMCommute 18:00 57 66 151
19:00 44 55 90
20:00 114 145 257
Create a chart of the traffic data in the timetable. First create a new figure window so that you do not reuse subplots that were created by tiledlayout
. Then, to plot data from the timetable, use stackedplot
. The stackedplot
function plots each timetable variable in a subplot and displays vertical black lines at the times that events occur.
figure stackedplot(TT)
Input Arguments
ts
— Input time series
array of timeseries
objects
Input time series, specified as an array of timeseries
objects.
This function uses some of the properties of ts
to either assign
data or set properties in the timetable. For each timeseries
property, the table describes the result in the output timetable.
Input | Result in Output Timetable |
---|---|
| Specifies the name of the corresponding timetable variable. If |
| Specifies the data assigned to the corresponding timetable variable. |
| Sets the |
| Sets the |
| Converts sample times to row times of the timetable. The vector of
row times is either a |
| Specifies units for row times. If the vector of timetable row times
is a |
| Sets the format for row times. |
| Sets the |
| Sets the |
| Calculates the offset from |
| Determines if the data needs to be reoriented. |
| Specifies the data assigned to the |
| Converts events to entries in an event table and attaches the event table to the timetable. (since R2024b) Duplicate events from the input time series result in duplicate rows in the event table. |
| Warns. |
| Warns if |
Version History
Introduced in R2021bR2024b: Convert events associated with timeseries
objects to an event table associated with a timetable
You can convert timeseries
objects that have events to a timetable that
has an event table. When timeseries2timetable
converts
timeseries
objects to a timetable, it also converts all associated
tsdata.event
objects to an event table. Then the function attaches the
event table to the timetable.
Before R2024b, timeseries2timetable
ignored events when converting
timeseries
objects to a timetable.
R2021b: timeseries2timetable
replaces ts2timetable
The timeseries2timetable
function replaces the
ts2timetable
function, although you can still use
ts2timetable
. The two functions are synonyms. In R2021a, MATLAB® provides ts2timetable
only.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)