MATLAB: For Loop using xlsread indexing

preallocatingr2020axlsread

The following loop is giving me a warning of "The variable 'raw' appears to change size on every loop iteration (within a script). Consider preallocating for speed."
for Str = {'Red' 'Green' 'Orange' 'Purple' 'Pink'};
folder = '';
FileNames=dir('.xls');
for i = length(FileNames)
FileToLoad = FileNames(i).name;
[~,~,raw{i}] = xlsread(FileToLoad);
if exist(FileToLoad , 'file')==0
continue;
end
end
return;
end
Also, when the files are read into the 'raw' container they are not in the same order as they are listed in the Str. I want the files to be listed in raw table in the order that they are listed in the Str. Is this possible, as I use those indexes later on in my code.
Any suggestions are appreciated. Thanks

Best Answer

basenames = {'Red' 'Green' 'Orange' 'Purple' 'Pink'};
nbase = length(basenames);
raw = cell(nbase, 1);
for K = 1 : nbase
FileToLoad = [basenames{K} '.xls'];
if exist(FileToLoad, 'file')
[~,~,raw{K}] = xlsread(FileToLoad);
end
end