import sequential files using for loop and sprintf

조회 수: 4 (최근 30일)
Ernest Adisi
Ernest Adisi 2018년 7월 27일
편집: Stephen23 2018년 7월 27일
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
LuKr
LuKr 2018년 7월 27일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 7월 27일
편집: Stephen23 2018년 7월 27일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by