MATLAB Answers

Find cell position of a timeseries array

13 views (last 30 days)
Ali Sahouli
Ali Sahouli on 11 Jan 2020
Commented: Meg Noah on 12 Jan 2020
Hello all,
we need to find the cell position of a timeseries which contains the time-value we are looking for. There is a big 1x1 double timeseries called test.
test.Time has all time-values and test.Data got signal-values. We need a start- and end-time for example: second 5 and second 10.
How is it possible to find the location of those cells which contains values close to 5 and 10. The timeseries are very big that is why maybe the time-values 5 or 10 are not in there but 5.025, 5.0042 or 10.05796 are possible to find.
A solution without a for loop is prefered.
Thank you for any help!

Accepted Answer

Meg Noah
Meg Noah on 11 Jan 2020
Assuming your data are in vector arrays that are, say, doubles, and packed as not cell arrays:
test.Time = 0:0.1:50;
test.Time = test.Time + 0.001*rand(size(test.Time));
test.Data = sin(2*pi*test.Time/25) + 0.3*rand(size(test.Time))-0.3*0.5;
startTime = 5;
endTime = 10;
idxStart = find(test.Time >= startTime,1,'first');
endTime = find(test.Time > endTime,1,'first') - 1;
hold on; ylabel('Data'); xlabel('Time (s)'); box on;
plot(test.Time(idxStart:endTime),test.Data(idxStart:endTime), ...
'DisplayName','Time Wanted');
plot([test.Time(idxStart) test.Time(idxStart)],[-1.5 1.5],':', ...
'DisplayName',['Start time = ' num2str(test.Time(idxStart)) ' s']);
plot([test.Time(endTime) test.Time(endTime)],[-1.5 1.5],':', ...
'DisplayName',['End time = ' num2str(test.Time(endTime)) ' s']);
Note: Indexing without using find is faster and better, but since you wanted to see the actual times of the start and stop, I used a find command.
Meg Noah
Meg Noah on 12 Jan 2020
You're very welcome. I should have mentioned that it's important to organize the time data monotonically increasing for this to work. You can use the sort function something like:
% test if the data are monotonically increasing
function tf = mono_increase(x)
tf = all(diff(x)>0);
% if not, sort the data
% find the index order that will result in monotonically increasing data
[~,idx] = sort(test.Time);
% then reorganize the data
test.Time = test.Time(idx);
test.Data = test.Data(idx)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by