how to read txt files in the same folder

조회 수: 3 (최근 30일)
San May
San May 2018년 12월 31일
댓글: Stephen23 2018년 12월 31일
I have text files in a folder. Their names are
BC_1.txt, BC_2.txt, BC_3.txt, ..., BC_100.txt.
I would like to know how to read all files with a loop. Thank you so much.
  댓글 수: 1
Stephen23
Stephen23 2018년 12월 31일
If you want to read the files in alpha-numeric order then you can either:
  1. generate the names using sprintf (as shown on the page madhan ravi linked to), or
  2. use dir to get the actual filenames, and then sort them. The simplest way to do this is to download my FEX submission natsortfiles, and use it like this:
S = dir('BC*.txt');
C = natsortfiles({S.name});
for k = 1:numel(C)
F = C{k}
... do whatever with filename F
end

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

채택된 답변

KSSV
KSSV 2018년 12월 31일
txtfiles = dir('*.txt') ;
N = length(txtfiles) ;
for i = 1:N
thisfile = txtfiles(i).name ;
% do waht you want
end

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 12월 31일

카테고리

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