For Loop using xlsread indexing

5 Ansichten (letzte 30 Tage)
James
James am 2 Jul. 2020
Kommentiert: Walter Roberson am 2 Jul. 2020
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 Kommentare
James
James am 2 Jul. 2020
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 am 2 Jul. 2020
Bearbeitet: dpb am 2 Jul. 2020
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Jul. 2020
Bearbeitet: Walter Roberson am 2 Jul. 2020
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 Kommentare
James
James am 2 Jul. 2020
perfect thank you. I just changed the raw{i} to raw{K}
Walter Roberson
Walter Roberson am 2 Jul. 2020
Sorry, yes, I did have that typo.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by