It looks like some indices contained in the code are outside the bounds of indices for “elProdTotArs”. Moreover, there are some typos and errors in the code. The error provided in the question means that the index “f” is going above the size of “elProdTotArS”, which has only 10 rows as can be seen at line number 25 (“elProdTotArS = zeros(10,2);” ).
To address this, please make the following adjustments in the code:
- Instead of pre-allocating “elProdTotArS” and “savedEl” with a fixed size of 10, it can be calculated using the actual number of years present in the data using:
years = stralning.data(end,1) - stralning.data(1,1) + 1;
elProdTotArS = zeros(years,2);
savedEl = zeros(years,2);
2. The while loop condition at line number 166 should be updated to prevent the index from exceeding the array size:
while elProdTot(k,1) == g && f < length(elProdTotArS)
3. There was a mismatch in variable names (“sparadEl “ vs “savedEl”). It should be corrected:
plot(savedEl(:, 1), savedEl(:, 2), 'o');
By making the above changes, the code is now able to handle cases, irrespective of any number of years in the data gracefully. Hope it helps.