"Index exceeds array bounds"
Show older comments
I have quite a simple for loop that goes into an excel file and collects a range of cells in every sheet of the file. The folder has 12 excel files and every file has a data for each month in each sheets. Need help Please
folder='2018';
filetype='*.xlsx';
f=fullfile(folder,filetype);
d=dir(f);
for i=1:numel(d);
filename=fullfile(folder,d(i).name);
[status,sheets] = xlsfinfo(filename);
for s=1:24 %numel(sheets);
output_0cm{s}=xlsread(filename,sheets{s},'D42:D42');
output_15cm{s}=xlsread(filename,sheets{s},'F42:F42');
output_22_8cm{s}=xlsread(filename,sheets{s},'H42:H42');
end
end
Mean_0cm=output_0cm{3:length(output_0cm)}
3 Comments
"...every file has a data for each month"
for s=1:24 %numel(sheets);
output_0cm{s}=xlsread(filename,sheets{s},'D42:D42');
There are only 12 months/year, not 24...
Why did you go away from using the returned size that you discovered for the sheets in lieu of the constant?
The performance of the above is likely not of serious concern, but in general it would be a lot quicker to write
data=xlsread(filename,sheets{s},'D42:H42');
and then just pick the desired three elements from the returned array. This is only opening/close the spreadsheet once instead of three times that is quite overhead-intensive.
Also, as you've written the above code, your result is going to overwrite the three arrays for each year leaving you with only the last year when done; you'll either need to allocate sufficient elements for all months of all years expected or do the processing on each year's worth of data after reading it and before going on to the next year.
madhan ravi
on 26 Dec 2018
Thank you for the good insight. The folder 2018 has 12 files in it representing each month of the year hence (for i=1:numel(d); will return 1-12.Each month has 1-24 sheets in it. My code goes into each sheet for each month and pulls out the required cells as shown. You are right though, the code will overide the returned data for the first 11 months and return just that for the last month on in the folder.
I don't quite understand how to fix this issue. I will like "output_0cm{s}" to have 12 rows, representing each month of the year. Right now im just getting 1 row and 24 coulumns for the month of December. Any suggestions?
dpb
on 27 Dec 2018
Well, then, there needs to be 12(mo) x 24(shts) x 3(vars) of storage available as data array or some other structure to hold that much data by indexing over the variables' ranges.
Just what would be most useful depends at least in part on what is the need...what is to be done with the 24 copies of what is, apparently, the same variable? Is that what is to be averaged?
Answers (0)
Categories
Find more on Logical 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!