read all text files in a directory

45 visualizaciones (últimos 30 días)
Shulamit Nussboim
Shulamit Nussboim el 12 de En. de 2023
Comentada: Sajid Afaque el 17 de En. de 2023
Hi,
I' like the code to read all the files in a directory, applying it manually it would have been:
load textfile1.txt
load textfile2.txt
load textfile3.txt
...
Thank you

Respuestas (2)

Sajid Afaque
Sajid Afaque el 12 de En. de 2023
Editada: Sajid Afaque el 16 de En. de 2023
try to use the below general approach
data_files=dir_listing(datapath,'*.txt') %reads all text files at the location specified by datapath
for e=1:numel(data_files)
%read the data from individual files
fid=fopen(fullfile(datapath,data_files{e}));
data_1=textscan(fid,'%s','delimiter','\n');
fclose(fid);
%then deal however you want to treat the data
end
  8 comentarios
Walter Roberson
Walter Roberson el 16 de En. de 2023
function files = dir_listing(folder, spec)
dinfo = dir(fullfile(folder, spec)) ;
files = {dinfo.name};
end
Sajid Afaque
Sajid Afaque el 17 de En. de 2023
Thanks walter. dir_listing would be a seperate function to list the names of all the files of particular format(here text files) from a specified directory

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 16 de En. de 2023
This is a FAQ, so see robust and general code snippets in the FAQ:

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by