Creating a loop in order to read .txt files

Hello,
I have files like these: A_ac_s001.txt , A_ac_s002.txt, A_ac_s003.txt ,..., A_ac_s100.txt
I would like to create a loop which will read 'one to one' each file. Does anyone knows which command should I use?

 Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Sept. de 2019
list=dir('*.txt');
for j=1:length(list)
file_text=textread(list(j).name);
%.....code
end

6 comentarios

Ivan Mich
Ivan Mich el 26 de Sept. de 2019
ok , there is a point too. in the file there are some .txt files with the name :
A_bb_s001.txt, A_bb_s002.txt, ...., A_bb_s100.txt . I would like to create a loop which will read without problem the previous files I've mentioned and the above files.
KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Sept. de 2019
Editada: KALYAN ACHARJYA el 26 de Sept. de 2019
Just run the code, it call file one by one (Expecting all text files in the current working directory)
Ivan Mich
Ivan Mich el 26 de Sept. de 2019
Editada: Ivan Mich el 26 de Sept. de 2019
sorry , I meant that finally i would like to create 2 loops, one for files A_ac_s001.txt , A_ac_s002.txt, A_ac_s003.txt ,..., A_ac_s100.txt
and one loop for files
A_bb_s001.txt, A_bb_s002.txt, ...., A_bb_s100.txt
in the same code
KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Sept. de 2019
Editada: KALYAN ACHARJYA el 26 de Sept. de 2019
One loop is sufficient to call all text files in the current directory, irrespective of any file name
"I meant that finally i would like to create 2 loops..."
So create two loops:
S = dir('A_ac*.txt');
for k = 1:numel(S)
S(k).name
end
S = dir('A_bb*.txt');
for k = 1:numel(S)
S(k).name
end
KALYAN ACHARJYA
KALYAN ACHARJYA el 26 de Sept. de 2019
Thanks @Stephen

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Sept. de 2019

Comentada:

el 26 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by