how to read txt files in the same folder

2 Ansichten (letzte 30 Tage)
San May
San May am 31 Dez. 2018
Kommentiert: Stephen23 am 31 Dez. 2018
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 Kommentar
Stephen23
Stephen23 am 31 Dez. 2018
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (1)

madhan ravi
madhan ravi am 31 Dez. 2018

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by