MATLAB: Find cell position of a timeseries array

cell arraysfindtimeseries

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 1×1 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!

Best Answer

  • 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.