import sequential files using for loop and sprintf

3 visualizaciones (últimos 30 días)
Ernest Adisi
Ernest Adisi el 27 de Jul. de 2018
Editada: Stephen23 el 27 de Jul. de 2018
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
  1 comentario
LuKr
LuKr el 27 de Jul. de 2018
I'm not sure if this is what you mean, but you can use the dir function to get all the files in your folder:
files = dir([<path to your file>,filesep,'B*.dat']);
filenames = {files.name};
and then loop through all filenames
for file = filenames
% your import function
end

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 27 de Jul. de 2018
Editada: Stephen23 el 27 de Jul. de 2018
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

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by