MATLAB: How can Looping (open text files, processing and save as Excel file) for multi times

faqMATLABprocess a sequence of files

Hi , Please , i have multi text files , i need looping : open ,processing, saving the results as excel file . repeat this process multi times based on number of text files , i have the code for open , processing , and save, process and save as excel for one file each time! so i should change text file name and excel file name munally each time !.
% How can repeat this process for multi text files ?
fid = fopen('stats1.txt','r');
strData = textscan(fid,'%s','Delimiter','\r\n');
strData = strData{1};
fclose(fid);
% Eliminate empty line(s) and the line(s) which contains '--'
idx1 = cellfun(@isempty,strData);
idx2 = contains(strData,'--');
strData = strData(~idx1 & ~idx2);
% Insert space before '#'
strData = replace(strData,'#',' #');
% Replace 3 or more consecutive spaces to 2 consecutive spaces
strData = regexprep(strData,'\s{3,}',' ');
% Prepare the output cell array
outData = cell(size(strData,1),6);
% Split each line with 2 consecutive spaces
for kk = 1:size(strData,1)
c = strsplit(strData{kk},' ');
outData(kk,[1:numel(c)-1, end]) = c;
end
% Save as Excel file
writecell(outData,'result1.xlsx');

Best Answer

Still i have problem with this insturaction : how can avoid that ?Please
%% Save result file
xlswrite(result,strcat(outDirectory,text_files(i).name));
Error using xlswrite:
File name must be a string scalar or character vector.
Error in:
xlswrite(outData,strcat(outDirectory,text_files(i).name));