Index in position 2 exceeds array bounds. Index must not exceed 27.

3 views (last 30 days)
I can't figure out why I am recieving an error message "Index in position 2 exceeds array bounds. Index must not exceed 27." when none of my INPUT matrices exceed 27 in position 2... INPUTS are made of zeros array to maintain the correct size of each array (other values are used in the actual code)
FINAL_OUTPUT = find_dbTP_Valley_Rim_JS(RFDB, unit_type, GIN_TEMPS_JS_STUCT, YYV_TEMPS_JS_STRUCT, YYV_PRECIP_JS)
%INPUTS
RFDB = zeros(1485,5)
unit_type = char('english')
GIN_TEMPS_JS_STUCT = struct;
GIN_TEMPS_JS_STUCT.name ='GIN';
GIN_TEMPS_JS_STUCT.elevation = 2149;
GIN_TEMPS_JS_STUCT.hourly = zeros(13047,27); %double array of 13047 rows and 27 columns
YYV_TEMPS_JS_STRUCT = struct();
YYV_TEMPS_JS_STRUCT.name ='YYV';
YYV_TEMPS_JS_STRUCT.elevation = 2149;
YYV_TEMPS_JS_STRUCT.hourly = zeros(80363,27); %double array of 80363 rows and 27 columns
YYV_PRECIP_JS = zeros(8046,27) %double array of 8046 rows and 27 columns
elev_rim = 2377; % 7800';
elev_valley = 1280; % 4200';
%FINAL CODE
S = size(RFDB);
L = S(1);
for i = 1:L
cur = RFDB(i, :);
year = cur(2);
month = cur(3);
day = cur(4);
time = cur(5);
if day == 0
% change the # of -100 depending on find_temp output length
RF_RIM_TEMPS = [-100, -100, -100, -100, 0];
RF_YYV_TEMPS = [-100, -100, -100, -100, 0];
RF_YYV_PRECIPS = [-100, -100, -100, -100];
RFDB(i, :) = [cur, RF_RIM_TEMPS, RF_YYV_TEMPS, RF_YYV_PRECIPS];
continue
elseif time < 0
time = 23/24;
end
date = datenum(year, month, day) + time;
datestr(date);
RF_RIM_TEMPS = find_temp_JS(date, unit_type, elev_rim, GIN_TEMPS_JS_STUCT);
RF_YYV_TEMPS = find_temp_JS(date, unit_type, elev_valley, YYV_TEMPS_JS_STRUCT);
YYV_RF_Precip = find_precip_JS(date, unit_type, YYV_PRECIP_JS);
JS_TP(i, :) = [cur, RF_RIM_TEMPS, RF_YYV_TEMPS, YYV_RF_Precip];
% Final OUTPUT^^
Note: Two codes shown below find_precip_JS and find_temp_JS are used within the final code "find_dbTP_Valley_Rim_JS"
1 of 2 Codes Used in Final Code: find_precip_JS
function[out_data] = find_precip_JS(date, unit_type, YYV_PRECIP_JS)
%% The objective is to find the precipitation of at the time of the rockfall
%
% out_data = find_precip(date, unit_type, precip_data)
%
% Inputs:
% precip data = [year, month, day, P_12am, P_1am, ..., P_11pm]
% unit_type = 'metric' for precip in centimeters
%
% out_data = [yearly cumulative, last 30 days, last 72 hours, last 24 hours]
%
%
% Requirements: Precip DATA FILE ASSUMED TO BE WELL-BEHAVED! No erroneous
% points
%
% ** Also, past 24 and past 72 hours may give bad data if the rock fall
% occurs on July 1, 2, or 3 AND it was raining (Data is set to zero).
%
%% Collect the data for the appropriate day
day = floor(date);
time = round((date-day)*24);
time_ind = 4 + time;
hourly = YYV_PRECIP_JS;
pyear = hourly(:, 1);
pmonth = hourly(:, 2);
pday = hourly(:, 3);
p_days = datenum(pyear, pmonth, pday);
ind = find(p_days == day);
if isempty(ind)
out_data = [-100, -100, -100, -100];
return
end
p_now = hourly(ind, time_ind); % rainfall now
p_yest = hourly(ind-1, time_ind); % rain at this time yesterday
p_24 = p_now - p_yest; % 24 hour rainfall
p_minus3 = hourly(ind-3, time_ind); % rain at this time 72 hrs ago
p_72 = p_now - p_minus3; % 72 hour rainfall
% sets data to zero if negative (will occur with good data on July 1
% (2, 3)), but this is wrong IF it was raining
if p_24 < 0
p_24 = 0;
end
if p_72 < 0
p_72 = 0;
end
dv = datevec(date);
%ind
if dv(2) == 7 %dv(2) is the month of the rf
% Code assumes a very well-behaved data file
dom = dv(3); % day of month
July1_ind = ind-dom+1;
%datestr(p_days(July1_ind))
pm1 = hourly(July1_ind, 4); %rainfall, july start -> should be ZERO!
%datestr(p_days(July1_ind-1))
pm2 = hourly(July1_ind-1, 4); %rainfall, june30
%datestr(p_days(ind-30))
pm3 = hourly(ind-30, 4);
p30a = p_now - pm1;
p30b = pm2 - pm3;
p_30 = p30a + p30b;
else
p_month = hourly(ind - 30, 4);
p_30 = p_now - p_month;
end
out_data = [p_now, p_30, p_72, p_24];
% convert to centimeters?
if strcmpi(unit_type,'metric')
out_data = out_data * 2.54;
end
2 of 2 Codes Used in Final Code: find_temp_JS
function[out_data] = find_temp_JS(date, unit_type, elev_valley, YYV_TEMPS_JS_STRUCT)
% Inputs:
% - date = matlab datenum
% - elevation = elevation, in meters, for which you desire temperature
% corrections
% - unit_type = 'metric' for celcius units 'english' for fahrenheit
% - YYV_TEMPS_JS_STRUCT or GIN_TEMPS_JS_STUCT= structure of temperature data, including:
% name = 'Weather Station Name'
% hourly = [year, month, day, T_12am, T_1am, ..., T_11pm]
% daily = [datenum, max, min, avg]
% elevation (in meters)
% days = list of datenums of all days in db
%
% out_data = [min, avg, max, temp at rock fall, # stations contributing data]
%% Constants
t_grad = 0.0;
%% Collect the data for the appropriate day
day = floor(date);
time = round((date-day)*24);
time_ind = 4 + time;
hour = [time+1:1:23, 0:1:time];
L = length(YYV_TEMPS_JS_STRUCT)
cur = [];
k = 1;
for i = 1:L
t_all = [];
hourly = YYV_TEMPS_JS_STRUCT(i).hourly
t_days = datenum(hourly(:, 1), hourly(:, 2), hourly(:, 3));
ind = find(t_days == floor(date));
% Create array of temps in the 24 hours prior to the rf (previous
% hi/lo)
if time_ind < 27
t_today = hourly(ind, 4:time_ind);
t_yesterday = hourly(ind-1, time_ind+1:27);
t_all = [t_yesterday t_today];
%t_h = [time-23:1:time];
else
%t_h = [0:1:23];
t_all = hourly(ind, 4:27);
end
%t_all
ind2 = t_all > -30;
t_all = t_all(ind2);
ind2 = t_all < 120;
t_all = t_all(ind2);
if isempty(t_all)
continue
% Do Nothing
else
%t_all
YYV_TEMPS_JS_STRUCT(i).name;
cur(k).t_max = max(t_all);
cur(k).t_min = min(t_all);
cur(k).t_avg = mean(t_all);
cur(k).elevation = YYV_TEMPS_JS_STRUCT(i).elevation;
end
%% Find the temperature nearest to the time of the rock fall
if time == 23 % this is a fake time, not appropriate for the rock fall
time = 21; % 7pm is a neutral time
time_ind = 4 + time;
end
% Create an array of the X hours surrounding the rock fall
plusminus = 6; % plusminus hours before and plusminus hours after
%time_ind
if time_ind > 27 - plusminus % late in the day
t_today = hourly(ind, time_ind-plusminus:27);
offset = plusminus - (27 - time_ind) - 1;
t_tomorw = hourly(ind+1, 4:4+offset);
t_near = [t_today t_tomorw];
elseif time_ind < 4 + plusminus % early in the day
t_today = hourly(ind, 4:time_ind+plusminus);
offset = plusminus - (time_ind - 4) - 1;
t_yesrdy = hourly(ind-1, 27-offset:27);
t_near = [t_yesrdy t_today];
else
t_near = hourly(ind, time_ind-plusminus:time_ind+plusminus);
end
t_h = [plusminus*-1:1:plusminus];
ind3 = t_near > -30;
t_near = t_near(ind3);
t_h = t_h(ind3);
ind3 = t_near < 120;
t_near = t_near(ind3);
t_h = t_h(ind3);
n_ind = find(t_h == 0);
% No data for time of rock fall
if isempty(t_near)
cur(k).t_rf = -100; % no data within plusminus hours of the rock fall
% Exact data not available, but before and after data are available
elseif isempty(n_ind)
b_ind = find(t_h < 0, 1, 'last');
a_ind = find(t_h > 0, 1, 'first');
% No before data: check to see if after data is close
close = 4; % Close is defined as less than "close" hours
if isempty(b_ind)
if t_h(a_ind) < close;
cur(k).t_rf = t_near(a_ind);
else
cur(k).t_rf = -100;
end
% No after data: check to see if before data is close
elseif isempty(a_ind)
if t_h(b_ind) > -1 * close
cur(k).t_rf = t_near(b_ind);
else
cur(k).t_rf = -100;
end
% Both before and after data: linear extrapolation
else
span = t_h(a_ind) - t_h(b_ind); % hours between before and after
temp_span = t_near(a_ind) - t_near(b_ind);
hr_incre = temp_span/span;
t_rf_tmp = t_near(b_ind) + (hr_incre * (t_h(b_ind)*-1));
cur(k).t_rf = t_rf_tmp;
end
else
cur(k).t_rf = t_near(n_ind); % exact data available, use it!
end
k = k + 1;
end
%% Evaluate the temperature at the appropriate elevation
if isempty(cur)
out_data = [-100, -100, -100, -100 0];
return
else
num_stat = length(cur);
end
%% It is possible to have min/max/avg data BUT NO temp at time of rock fall data
m = 1;
t_rf = -100;
for j = 1:num_stat
t_elev = cur(j).elevation;
t_diff = ((t_elev - elev)/100) * t_grad;
t_max(j) = (cur(j).t_max + t_diff);
t_min(j) = (cur(j).t_min + t_diff);
t_avg(j) = (cur(j).t_avg + t_diff);
t_rf_tmp = cur(j).t_rf;
if t_rf_tmp == -100
% Do nothing
else
t_rf(m) = (t_rf_tmp + t_diff);
m = m + 1;
end
end
  3 Comments
John Stapke
John Stapke on 19 Jan 2022
Index in position 2 exceeds array bounds. Index must not exceed 27.
Error in find_precip_JS (line 38)
p_now = hourly(ind, time_ind); % rainfall now
Error in find_dbTP_Valley_Rim_JS (line 56)
YYV_RF_Precip = find_precip_JS(date, unit_type, YYV_PRECIP_JS);
Stephen23
Stephen23 on 19 Jan 2022
I don't see anything in the code of find_precip_JS that limits the value of time_ind to 27 or less.
You should check the size of hourly and time_ind using the debugging tools. If you have not used the debugging tools then this is the perfect time to start. The most useful command to begin is
dbstop if error
and then simply check the array size and index value. Let us know what the index value is.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 19 Jan 2022
Somewhere in your code, you have a matrix of nx27, but in your code, you are trying to access a column > 27.
a=rand(3,27);
% Works
a(2,20)
ans = 0.3973
% Your error
a(2,28)
Index in position 2 exceeds array bounds. Index must not exceed 27.
If you look at the full error message, it will tell you what line of code is giving this error.

Community Treasure Hunt

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

Start Hunting!