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
  1 件のコメント
Stephen23
Stephen23 2023 年 1 月 12 日
編集済み: Stephen23 2023 年 1 月 12 日

サインインしてコメントする。

回答 (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 Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by