MATLAB: Continue loop on other files if current file is deleted within a loop

else ifif statementloop

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

Best Answer

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 ?