For Loop using xlsread indexing

조회 수: 4 (최근 30일)
James
James 2020년 7월 2일
댓글: Walter Roberson 2020년 7월 2일
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에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by