How to deal with Index exceeds the number of array elements ?

6 views (last 30 days)
Hi all,
I have a signal like this (also attached).
I want to extract signal between first start and first end, second start and second end and so on. I am doing this like this:
load 'signal'
start_location = [147,222,302,379,458,538,620,698,777];
start_value = [-81.905,-87.408,-86.262,-85.463,-85.893,-84.910,-84.614,-83.543,-84.500]
end_location = [200,275,352,431,511,593,674,762];
end_value = [-85.369,-85.028,-82.240,-80.933,-80.943,-80.082,-83.108,-85.543];
plot(thigh_orient_y,'k')
hold on
plot(start_location,start_value,'ro')
plot(end_location,end_value,'g*')
h = legend('signal','start', 'end');
%Settings
a = start_location ;
b = end_location;
n = length(a);
%Extract data between red and green indices and put in cells
T_or_y_cycle = cell(n,1) ;
for i = 1:n
T_or_y_cycle{i} = thigh_orient_y(a(i):b(i)) ;
end
I am getting error
Index exceeds the number of array elements
Beacuse there is larger number of 'start' than 'end', as the signal is cut at the end. Can you please help how to deal with this?
The n cannot be lenght(b) beacuse the signal can be cut at the begging and start with 'end' like here:
  2 Comments
Ankit
Ankit on 31 Jan 2022
Edited: Ankit on 31 Jan 2022
How about choosing the size based on minimum value of n = min(length(a),length(b)) ?
Tomaszzz
Tomaszzz on 31 Jan 2022
Edited: Tomaszzz on 31 Jan 2022
Thanks, this does the trick, how can I accept you answer?

Sign in to comment.

Accepted Answer

Ankit
Ankit on 31 Jan 2022
load 'signal'
start_location = [147,222,302,379,458,538,620,698,777];
start_value = [-81.905,-87.408,-86.262,-85.463,-85.893,-84.910,-84.614,-83.543,-84.500]
end_location = [200,275,352,431,511,593,674,762];
end_value = [-85.369,-85.028,-82.240,-80.933,-80.943,-80.082,-83.108,-85.543];
plot(thigh_orient_y,'k')
hold on
plot(start_location,start_value,'ro')
plot(end_location,end_value,'g*')
h = legend('signal','start', 'end');
%Settings
a = start_location ;
b = end_location;
n = min(length(a),length(b)); % this will avoid extra start or end value.
%Extract data between red and green indices and put in cells
T_or_y_cycle = cell(n,1) ;
for i = 1:n
T_or_y_cycle{i} = thigh_orient_y(a(i):b(i)) ;
end

More Answers (1)

KSSV
KSSV on 31 Jan 2022
Your start_location is of size 1x9. Where as end_location is only 1x8. You need to put one more index in the variable end_location.

Community Treasure Hunt

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

Start Hunting!