How can I get matlab to go through the list "time" and give all the indexes that are between my upper and lower range for "times"

2 views (last 30 days)
This is my code so far. Essentially, I need to find plus and minus 5 seconds from each data point in array_times_L, and then find the corresponding indices in the array time so that I can plot the signal at each of those indices. This is my code so far, please let me know if there's anything I need to clarify.
array_mouse_L_star = table2array(mouse_L_star)
time=array_mouse_L_star(:,2)
signal=array_mouse_L_star(:,1)
array_times_L = table2array(L_times)
%times when mouse took a bite
times = array_times_L;
lowrange=times-5;
upperrange=times+5;
%find index for times ranges when mouse took a bite
[time_idx,times_idx] = find(time(:) > lowrange & time(:) < upperrange)
signal = array_mouse_r_stick(time_idx)
%split signals into columns for each time point
allsignals = reshape(signal, 100, [])
%average across each row
avg = mean(allsignals,2)
%create x values from minus 5 to plus 5 seconds
x=(linspace(-5,5,100))'
plot(x,avg,"b-")
xlabel("Time(s)")
ylabel("Signal")
my main issue is with line 10 because it keeps saying the array do not match dimensions
  1 Comment
Eric Sofen
Eric Sofen on 21 Jun 2022
It looks like Voss got you unstuck, but another approach is to put your data in a timetable and use the withtol subscripters to extract the data around particular times.

Sign in to comment.

Accepted Answer

Voss
Voss on 20 Jun 2022
Edited: Voss on 20 Jun 2022
Try this:
[time_idx,times_idx] = find(time > lowrange.' & time < upperrange.')
%
% ^^^^ column vector, i.e., one value per row
% ^^^^^^^^^^ row vector, i.e., one value per column
%
%^^^^^^^^ row index
% ^^^^^^^^^ column index
time_idx will be the indices of the rows where the condition given to find is true; times_idx will be the columns.
Therefore, the matrix given to find must have rows that correspond to time and columns that correspond to times.
As it is now, time and times (and therefore lowrange and upperrange) are column vectors (looks like), so using the transpose of lowrange and upperrange makes the expression
time > lowrange.' & time < upperrange.'
a matrix whose rows correspond to elements of time and whose columns correspond to elements of times (and therefore lowrange and upperrange).
(Note that the situation is different from your previous question, where everything was row vectors, so only time needed to be converted into a column vector, which was done using time(:).)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!