MATLAB: Import sequential files using for loop and sprintf

for loop import multiple files *dat files

i'm trying to import sequential files with filename format B00001.dat to B00050.dat. Im reading the 'sprintf' function but don't understand how it works to sequentially import all the files one-by-one from the folder. Can someone explain how this please? Thanks Ernest

Best Answer

Using a for loop and sprintf, exactly as the question title requests:
V = 1:50; % the file numbers that you want to import
N = numel(V)
C = cell(1,N);
for k = 1:N
F = sprintf('B%05d.dat',V(k));
C{k} = function_that_reads_your_file(F);
end