# running for loop gives error " Row index exceeds table dimentions "

1 view (last 30 days)
taimour sadiq on 7 Sep 2021
Commented: taimour sadiq on 7 Sep 2021
i want to extraxt specific range of data from a table names as "Raw", and finally find mean of selected data assigned to variable answer. the below codes works frine for single range, but i want to mention an array of min & max range and so that when a for loop runs it should select new range on every iteration but it gives error " Row index exceeds table dimentions .
Raw = table([1 2 3 4 5 6 7 8 9]',[3.6 3.4 5.3 6.8 8.4 3.6 4.8 8.8 7.3]') % the simple code works fine
minrange = 1; maxrange = 3;
data = table2array(varfun(@(x)((x>=minrange)&(x<=maxrange)), Raw(:,1)));
data = Raw(data,:)
##### #######
Raw = table([1 2 3 4 5 6 7 8 9]',[3.6 3.4 5.3 6.8 8.4 3.6 4.8 8.8 7.3]') % this code gives error when minrange changed
minrange = [1 4]; maxrange = [3 6];
for i = 1:2
data = table2array(varfun(@(x)((x>=minrange)&(x<=maxrange)), Raw(:,1)));
data = Raw(data,:)
end

Abolfazl Chaman Motlagh on 7 Sep 2021
your minrange and maxrange now are vecotors with two value.if you want to put it in for loop, in data you should mention wich component you want.
Raw = table([1 2 3 4 5 6 7 8 9]',[3.6 3.4 5.3 6.8 8.4 3.6 4.8 8.8 7.3]') ; % this code gives error when minrange changed
minrange = [1 4]; maxrange = [3 6];
for i = 1:2
data = table2array(varfun(@(x)((x>=minrange(i))&(x<=maxrange(i))), Raw(:,1)));
data = Raw(data,:);
end
4.1000 6.2667
or you could :
Raw = table([1 2 3 4 5 6 7 8 9]',[3.6 3.4 5.3 6.8 8.4 3.6 4.8 8.8 7.3]') ; % this code gives error when minrange changed
minrange = [1 4]; maxrange = [3 6];
data = table2array(varfun(@(x)((x>=minrange)&(x<=maxrange)), Raw(:,1)));
for i = 1:2
data_ = Raw(data(:,i),:);
end
4.1000 6.2667
but for an easier way to do this, i suggest: (don't use table)
Raw = [3.6 3.4 5.3 6.8 8.4 3.6 4.8 8.8 7.3]';
minrange = [1 4]; maxrange = [3 6];
for i=1:2
end
4.1000 6.2667
taimour sadiq on 7 Sep 2021
Thanks Abolfazl Chaman Motlagh, ur Answer is exactly according to my Requirements, especially u guide with multipe method that is very helpful for me and hope for others also.

R2021a

### Community Treasure Hunt

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

Start Hunting!