Continue loop on other files if current file is deleted within a loop?
1 次查看(过去 30 天)
显示 更早的评论
I want to delete *.xlsx files if they meet a certain criteria. However when a file is deleted based on the first if statement it continues to try and look though the files for the other criteria. How can bypass the remaining if statements and continue onto the next file if one of the if statements are satisfied?
excel_xlsx=dir('*.xlsx'); % list of the files
for i=1:length(excel_xlsx)
[~,file]=xlsfinfo(excel_xlsx(i).name);
L=length(file);
[~,txt]=xlsread(excel_xlsx(i).name,L,'B2'); % read the text
if strcmp(txt,'Number:')
delete(excel_xlsx(i).name)
else
[~,txt]=xlsread(excel_xlsx(i).name,1,'B3'); % read the text
strcmp(txt,'PROGRAM')
delete(excel_xlsx(i).name)
else
[~,txt]=xlsread(excel_xlsx(i).name,1,'A10'); % read the text
strcmp(txt,'UPPER')
delete(excel_xlsx(i).name)
else
[~,txt]=xlsread(excel_xlsx(i).name,1,'A2'); % read the text
strcmp(txt,'Effect')
[~,sheets] = xlsfinfo(excel_xlsx(i).name);
xlsprotect(excel_xlsx(i).name,'unprotect_sheet',sheets{1,1});
xlswrite(excel_xlsx(i).name,' ',1,'F1');
xlswrite(excel_xlsx(i).name,' ',1,'F2');
xlswrite(excel_xlsx(i).name,' ',1,'F3');
xlswrite(excel_xlsx(i).name,' ',1,'E1');
xlswrite(excel_xlsx(i).name,' ',1,'E2');
xlswrite(excel_xlsx(i).name,' ',1,'E3');
xlswrite(excel_xlsx(i).name,' ',1,'E4');
end
end
0 个评论
采纳的回答
Walter Roberson
2017-7-16
编辑:Walter Roberson
2017-7-16
excel_xlsx=dir('*.xlsx'); % list of the files
for i=1:length(excel_xlsx)
[~,file]=xlsfinfo(excel_xlsx(i).name);
L=length(file);
[~,txt]=xlsread(excel_xlsx(i).name,L,'B2'); % read the text
if strcmp(txt, 'Number:')
delete(excel_xlsx(i).name);
continue;
end
[~,txt]=xlsread(excel_xlsx(i).name,1,'B3'); % read the text
if strcmp(txt, 'PROGRAM')
delete(excel_xlsx(i).name);
continue;
end
[~,txt]=xlsread(excel_xlsx(i).name,1,'A10'); % read the text
if strcmp(txt, 'UPPER')
delete(excel_xlsx(i).name);
continue;
end
[~,txt]=xlsread(excel_xlsx(i).name,1,'A2'); % read the text
if strcmp(txt, 'Effect')
[~,sheets] = xlsfinfo(excel_xlsx(i).name);
xlsprotect(excel_xlsx(i).name,'unprotect_sheet',sheets{1,1});
xlswrite(excel_xlsx(i).name,' ',1,'F1');
xlswrite(excel_xlsx(i).name,' ',1,'F2');
xlswrite(excel_xlsx(i).name,' ',1,'F3');
xlswrite(excel_xlsx(i).name,' ',1,'E1');
xlswrite(excel_xlsx(i).name,' ',1,'E2');
xlswrite(excel_xlsx(i).name,' ',1,'E3');
xlswrite(excel_xlsx(i).name,' ',1,'E4');
end
end
However, having all of those xlswrite is inefficient, especially for .xlsx files, as each one can involve rewriting the entire text of the xml file inside the .xlsx archive.
It would be more efficient to read all of the data at the same time. The way reading .xlsx files is implemented, when you put a restriction on which cells to read, that is handled by reading the entire .xml file and throwing away the unwanted cells, so you would be more efficient to just have one xlsread(), do the appropriate tests, modify the appropriate cells, and write the result out if it changed.
Note by the way that you read in the list of sheets early on, so it is not efficient to do a second xlsinfo() .
Could you confirm that you want to read data from the last sheet, but that it is the first sheet that you want to modify E1:F3 and E4 of ?
2 个评论
Walter Roberson
2017-7-16
It is possible that reading specific cells from .xls files is efficient on MS Windows systems with Excel installed.
But .xlsx files are actually zip archives with several directories and a number of .xml files, and the way MATLAB handles reading them is to use regexp to pull out all occurrences of the appropriate data structures and then later to look through what it found to figure out which parts to keep. This process is faster than parsing the .xml text file line by line to figure out whether the entries on the line are part of what should be kept, because doing that testing at the MATLAB level gets expensive.
更多回答(1 个)
Image Analyst
2017-7-16
You can skip the contents of the loop and restart at the top of the loop with the next index by using the continue command.
But I don't understand two things. You're calling xlsread() right after you deleted the file? Why? It won't be there to read! Secondly, why do you need to restart the loop, either with the next index or with an index of i=1? Why??? I don't see any need for that at all. Explain why you think that is necessary.
4 个评论
Image Analyst
2017-7-16
If you have R2015b (I believe that's the version) or later, calling xlsread 3 or 4 times won't take much longer than calling it once since the Excel server is left running and it's simply memory transfer, you don't have to launch and shutdown Excel like you did in earlier versions which took a lot of time.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!