dinfo = dir('*.txt');
filenames = {dinfo.name};
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
newname = basename + ".xlsx";
data = readmatrix(thisfile);
writematrix(data, newname);
end
This code assumes that you do not have any header in the file -- if you have a variable name for each column then switch to readtable() and writetable()
This code assumes that all of the columns are the same type -- if not, then switch to readtable() and writetable(), possibly with 'writevariablenames', false if there should not be a header line.
This code assumes that the .txt file format is something that readmatrix() can guess at.