MATLAB: Is adding many events to a time series object so slow in MATLAB 7.8 (R2009a)

MATLABtimeseries

I have code which adds hundreds of events to my time series object, and the code takes a very long time to complete. Here is an example of the code I am running:
clear all
Step = 0.1;
TS = timeseries( 1:Step:100, 1:Step:100 );
EventTimeStamp = 20:Step:80;
Length = size( EventTimeStamp, 2 );
tic
for i = 1:Length
TS = addevent(TS, 'Data', EventTimeStamp(i));
end
toc

Best Answer

A time series object's 'Events' property holds an array of tsdata.event objects. Each time ADDEVENT is called, another tsdata.event object is created and added to the array. However before the new tsdata.event object is added to the array, the ADDEVENT method first checks to see that the new event is not a duplicate of an event that already exists in the array. As the the array grows larger each new event must be checked against a longer list of existing events, and therefore each subsequent event takes longer to add than the last.
To work around this you may modify the 'Events' property of you time series object directly and forgo the error checking if your data permits it. For example:
clear all
Step = 0.1;
TS = timeseries( 1:Step:100, 1:Step:100 );
EventTimeStamp = 20:Step:80;
Length = size( EventTimeStamp, 2 );
tic
for i = 1:Length
TS.Events = [TS.Events, tsdata.event('Data', EventTimeStamp(i))];
end
toc