For Loop using xlsread indexing

2 次查看(过去 30 天)
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
  3 个评论
James
James 2020-7-2
I take the indexes of the .xls files after I xlsread the files into raw. Then those indexes of the raw I process later on in my code. They maybe in my folder ie( Orange.xls, Pink.xls, Red.xls, Green.xls) But I want them listed in the raw table as they are designated in the Str:
Red.xls
Green.xls
Orange.xls
Purple.xls
Pink.xls
Also, I want to load Red.xls then Green.xls then Orange.xls then Purple.xls and then Pink.xls. If lets say, Orange.xls is not in the folder, then I want to leave that index for Orange.xls empty and move on to find the remaining files.
For Example in the raw table if Orange.xls was missing in folder:
index 1 Red.xls
index 2 Green.xls
index 3
index 4 Purple.xls
index 5 Pink.xls
I use the indexes of the files later on in my code.
dpb
dpb 2020-7-2
编辑:dpb 2020-7-2
Well, it will stop when it's run a maximum of length(FileNamess) times; it's a counted loop. Of course, that could be a sizable number depending on what FileNames contains.
length is risky depending -- altho if one presumes based on use of the .Name field FileNames is the result of a dir() call (did you use wildcard to eliminate the "., .." directory entries?) it is a 1D struct array so you get what you expect. Read the documentation for length to see why it's not good in general.
The exist test is pretty-much pointless; dir() won't return an entry for a non-existing file so if you use something like
d=dir(fullfile('directoryString'),'*.xlsx');
for i=1:numel(d)
..
end
you'll only have the files with .xlsx extension; refine the wildcard expression to be more selective.
As far as the raw, save a variable, but the raw data will be a cell array of the size of the elements in the worksheet; it would probably be better to process each in turn before going on to the next--otherwise, you'll have to do something like create a 3D cell array or a cell array of cell arrays.
You also should probably look at and seriously consider readtable and returning the data as MATLAB table instead of the raw cell data.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-7-2
编辑:Walter Roberson 2020-7-2
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
  2 个评论
James
James 2020-7-2
perfect thank you. I just changed the raw{i} to raw{K}
Walter Roberson
Walter Roberson 2020-7-2
Sorry, yes, I did have that typo.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by