Error: Index out of bounds
2 views (last 30 days)
Show older comments
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
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
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!