1
votes

I am looking for a simple and fast way to get the index of an entry of a Time Series Object. For example:

%ts is a time series object with the following properties:
% Common Properties:
        Name: 'unnamed'
        Time: [70001x1 double]
    TimeInfo: [1x1 tsdata.timemetadata]
        Data: [70001x1 double]
    DataInfo: [1x1 tsdata.datametadata]

%first entry of ts is: 0 (time), 0.0667 (data)    
%second entry of ts is: 0.01 (time), 0.0667 (data)
%adn so on...

%I'm looking for an index i, such that i is the entry at the time 500.00
indexEntry = ts.time(i);
result = indexEntry == 500.00
%and resut should be true
2
Please add some sample data in the exact (MATLAB syntax) format that you are using. Also, are you looking to match floating point values or integers because == is not reliable for finding floats... - Dan
I don't understand your example, using indexEntry = ts.time(i); you get the you use the value as an index: ts.time(indexEntry). - Daniel
I just revised my question. I hope it is now more clearer. - d4rty
Not really, I don't get the meaning of ts.time(ts.time(i)), which is effectively what you have written. - Daniel
@Daniel That was a mistake. I just revised it. - d4rty

2 Answers

1
votes
tmp = 1:numel(ts.Time);
index = tmp(ts.time==500); %// 7000-by-1 logical array

The time at Time==500 can be found using logical indexing.

Following @Daniel's comment the following should be faster for single indices:

index = find(ts.time==500);
0
votes

Need to pay attention to the floating point data type.

index = find(abs(ts.time-500)<0.00001);