read all text files in a directory
41 次查看(过去 30 天)
显示 更早的评论
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
1 个评论
回答(2 个)
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
2023-1-16
function files = dir_listing(folder, spec)
dinfo = dir(fullfile(folder, spec)) ;
files = {dinfo.name};
end
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!