How can i read files inside a folder?

18 ビュー (過去 30 日間)
Gn Gnk
Gn Gnk 2022 年 1 月 27 日
コメント済み: Gn Gnk 2022 年 1 月 27 日
Hello ,
i have a folder that contains some .dat file . How can i open this folder and then read(for example) the first 4 .dat files ?
Any help would be valuable .
*i dont need to just diplay the files , i need the actual .dat files to be opened and then work with the data inside them .

採用された回答

Walter Roberson
Walter Roberson 2022 年 1 月 27 日
foldername = 'SnowCoverJuly2000';
dinfo = dir( fullfile(foldername, '*.dat') );
dinfo(5:end) = []; %only want first 4
nfiles = length(dinfo);
for K = 1 : nfiles
thisfile = fullfile(dinfo(K).folder, dinfo(K).name);
open(thisfile);
end
You might have problems if you have not installed a third-party program that is registered as being able to open .dat files.
  3 件のコメント
Gn Gnk
Gn Gnk 2022 年 1 月 27 日
Also i get this error : Reference to non-existent field 'folder'.
Gn Gnk
Gn Gnk 2022 年 1 月 27 日
I figured it out .Because the .folder extension was added after 2016b release i had this problem. So i changed the code to:
foldername = 'SnowCoverJuly2000';
dinfo = dir( fullfile(foldername, '*.dat') );
dinfo(5:end) = []; %only want first 4
nfiles = length(dinfo);
for K = 1 : nfiles
thisfile = fullfile(foldername, dinfo(K).name);
open(thisfile);
end
and it worked perfectly. Thank your for your help.

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

その他の回答 (1 件)

KSSV
KSSV 2022 年 1 月 27 日
thepath = 'giveyourpathhere' ;
files = dir([thepath,filesep,'*.dat']) ; % get .dat files in the folder
N = length(files) ; % total dat files present in the folder
for i = 1:N
thisfile = fullfile(files(i).folder,files(i).name) ; % get each file
% load the file
% do what ever you want
end
  1 件のコメント
Gn Gnk
Gn Gnk 2022 年 1 月 27 日
Thank you for your answer ,but it seems that there is not a files.fodler field .I get this error :
Reference to non-existent field 'folder'.

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

カテゴリ

Help Center および File ExchangeAgriculture についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by