Continue loop on other files if current file is deleted within a loop?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 16 de Jul. de 2017
0 votos
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 comentarios
Image Analyst
el 16 de Jul. de 2017
OK, good, now the xlsread() line is not the very next line of code after the delete() line like it used to be.
The file will not be read (or try to get read) after you delete it because each filename in excel_xlsx structure is looked at exactly once (to read or delete) and never again.
Explain exactly how you think you might be calling xlsread() on a file after it's been deleted, because I don't see how that's possible, unless you have a second, unshown, loop where you don't update excel_xlsx and do the loop using the old excel_xlsx, which would be a bad idea.
Calabrese
el 16 de Jul. de 2017
Image Analyst
el 16 de Jul. de 2017
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.
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!