Clear Filters
Clear Filters

Error: Index out of bounds

2 views (last 30 days)
ANOSH PHIROZ AMARIA
ANOSH PHIROZ AMARIA on 11 Sep 2018
Commented: madhan ravi on 11 Sep 2018
I am running the following MATLAB code to get the weekly average of data in 52704 columns. The data is getting imported correctly, but the last column in the array of week_speed is not getting filled.
month=xlsread('2004.xlsx','11517-2004','B2: B52705'); % Extracting month
day=xlsread('2004.xlsx','11517-2004','C2: C52705');% Extracting day
seconds=xlsread('2004.xlsx','11517-2004','F2: F52705');%Extracting seconds
wind_speed=xlsread('2004.xlsx','11517-2004','H2: H52705');%Extractng wind speed
index=1;
week_speed=zeros(53,1);
average=zeros(53,1);
week=1;
incr=1;
index_avg=1;
count=zeros(53,1);
while incr<=52704
while week<55
while index<=1008
week_speed(week)=week_speed(week)+wind_speed(incr);
index=index+1;
incr=incr+1;
end
average(index_avg)=week_speed(week)./1008;
count(week)=week;
index_avg=index_avg+1;
index=1;
week=week+1;
end
end
plot(count,average);
Error Displayed:
Attempted to access wind_speed(52705);
index out of bounds because numel(wind_speed)=52704.
  8 Comments
ANOSH PHIROZ AMARIA
ANOSH PHIROZ AMARIA on 11 Sep 2018
ohh so sorry I uploaded the wrong format. Just attached the right one
madhan ravi
madhan ravi on 11 Sep 2018
first of all explain what you are doing inside the loop it's all scattered and hard to interpret

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 11 Sep 2018
Edited: madhan ravi on 11 Sep 2018
[num,txt,raw]=xlsread('11517_2004.xlsx')
month=num(:,2)
day=num(:,3)
seconds=num(:,6)
wind_speed=num(:,8)
week_speed=zeros(numel(wind_speed),1);
for i=1:numel(wind_speed)
count(i)=i;
week_speed(i)=week_speed(i)+wind_speed(i);
average(i)=week_speed(i)./1008;
end
plot(count(1:54-1),average(1:54-1))
  1 Comment
madhan ravi
madhan ravi on 11 Sep 2018
If it's not what you are looking for , let know.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!