read all text files in a directory

조회 수: 45 (최근 30일)
Shulamit Nussboim
Shulamit Nussboim 2023년 1월 12일
댓글: Sajid Afaque 2023년 1월 17일
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

답변 (2개)

Sajid Afaque
Sajid Afaque 2023년 1월 12일
편집: Sajid Afaque 2023년 1월 16일
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
Walter Roberson
Walter Roberson 2023년 1월 16일
function files = dir_listing(folder, spec)
dinfo = dir(fullfile(folder, spec)) ;
files = {dinfo.name};
end
Sajid Afaque
Sajid Afaque 2023년 1월 17일
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

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


Image Analyst
Image Analyst 2023년 1월 16일
This is a FAQ, so see robust and general code snippets in the FAQ:

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by