Error reading excel sheet: index out of bounds.

Hi all,
Sorry to bother. I'm stuck on a bit of code. It worked initially with one column of data, but when I tried to use a different column, I got this error message:
Attempted to access yearsh(30133); index out of bounds because numel(yearsh)=30132.
Error in SeasonalCorrection (line 16) year = yearsh(i); %Names variables for each iteration
Problematic part of the code:
%Opens the file you listed and takes the four columns individually
yearsh = xlsread(filename,sheetnumber,yearcol);
monthsh = xlsread(filename,sheetnumber,monthcol);
daysh = xlsread(filename,sheetnumber,daycol);
icesh = xlsread(filename,sheetnumber,icecol);
%Creates a cell array with enough openings for each data point
edited = cell(range,5);
%Takes each data point individually
for i = 1:range; year = yearsh(i); %Names variables for each iteration month = monthsh(i); day = daysh(i); ice = icesh(i);
The excel sheet it's referencing has holes in the data, but this didn't cause any issue with the first column I tried, though it also had its fair share of holes in it. The holes are just blanks, not zeros. Later they're taken out of the sheet, and that didn't cause any issues with the first column I tried.
Any idea what's going on? I'd like for it to be generic enough that it can take in any excel sheet and any columns and run the operations, but it seems to want to stay put on the first column...
Thanks!

 Accepted Answer

What is range? Apparently it's bigger than 30132, the size of yearsh.

4 Comments

Range is the number of data points in the excel file (rows), here it's 31697. Ironically year has data in each of those rows: no blanks, the new column (icesh) does have blanks. Same data sheet with same range and same year column had no problems when I ran it with a different ice column.
It's hard to say, but this is where the debugger becomes really helpful.
Run the following at the command prompt:
>>dbstop if error
And then run your script. It will stop when the error occurs so that you can inspect the variables.
I think I understand. So I ran it a few other times, and the yearsh never seemed to be a problem again, now it's just icesh. Which kind of makes sense, as it has blanks in the beginning in the excel sheet, and it skips over those when moving into matlab. I just need to find a way to hold their places and just leave it blank when it gets read over, preferably putting them in as NaN... Thank you so much! I've never used dpstop before.
If you use the import tool to import the data, you can change how it handles unimportable cells.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!