Find cell position of a timeseries array

조회 수: 14 (최근 30일)
Ali Sahouli
Ali Sahouli 2020년 1월 11일
댓글: Meg Noah 2020년 1월 12일
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!

채택된 답변

Meg Noah
Meg Noah 2020년 1월 11일
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;
figure()
hold on; ylabel('Data'); xlabel('Time (s)'); box on;
plot(test.Time,test.Data,'DisplayName','Data');
plot(test.Time(idxStart:endTime),test.Data(idxStart:endTime), ...
'DisplayName','Time Wanted');
legend('location','best');
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']);
findingTime.png
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.
  댓글 수: 2
Ali Sahouli
Ali Sahouli 2020년 1월 12일
Thank you very much!
Meg Noah
Meg Noah 2020년 1월 12일
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);
end
% 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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by