フィルターのクリア

Loop through sub folders.

7 ビュー (過去 30 日間)
AUWAL ABUBAKAR
AUWAL ABUBAKAR 2020 年 1 月 30 日
コメント済み: AUWAL ABUBAKAR 2020 年 1 月 30 日
Good day all,
I have 25 subfolder each contains some text files. I developed a loop that runs through the text files in a sub folder, extract some vaules from each text file and perform some calculations. Is there any way i can modify my code to run through all the the sub folders at the same time? I want the loop to execute my script (see the script below) for each sub folder and save the results ('Displacement') before proceeding to the next subfolder. So i am expeting to have Displacement1, Displacement2, ..Displacement25 as the final outcome.
x = []; y = []; z = [];
files = dir('*.xvi');
for k = 1:length(files);
fid = fopen(files(k).name,'r');
while ~feof(fid)
st = fgetl(fid);
if ~isempty(strfind(st,'CouchShiftLat'))
stread = textscan(st,'%s %f','Delimiter',';=');
x = [x; stread{2}(1)];
elseif ~isempty(strfind(st,'CouchShiftLong'))
stread = textscan(st,'%s %f','Delimiter',';=');
y = [y; stread{2}(1)];
elseif ~isempty(strfind(st,'CouchShiftHeight'))
stread = textscan(st,'%s %f','Delimiter',';=');
z = [z; stread{2}(1)];
end
end
fclose(fid);
end
Displacement=[x,y,z];

採用された回答

Mohammad Sami
Mohammad Sami 2020 年 1 月 30 日
編集済み: Mohammad Sami 2020 年 1 月 30 日
mainfolder = uigetdir;
subfolders = dir(mainfolder);
subfolders = subfolders([subfolders.isdir] & ~startsWith({subfolders.name},'.'));
for i = 1:length(subfolders)
x = []; y = []; z = [];
files = dir(fullfile(mainfolder,subfolders(i).name,'*.xvi'));
for k = 1:length(files);
fid = fopen(files(k).name,'r');
while ~feof(fid)
st = fgetl(fid);
if ~isempty(strfind(st,'CouchShiftLat'))
stread = textscan(st,'%s %f','Delimiter',';=');
x = [x; stread{2}(1)];
elseif ~isempty(strfind(st,'CouchShiftLong'))
stread = textscan(st,'%s %f','Delimiter',';=');
y = [y; stread{2}(1)];
elseif ~isempty(strfind(st,'CouchShiftHeight'))
stread = textscan(st,'%s %f','Delimiter',';=');
z = [z; stread{2}(1)];
end
end
fclose(fid);
end
Displacement=[x,y,z];
% save here
end
  5 件のコメント
Mohammad Sami
Mohammad Sami 2020 年 1 月 30 日
yes, put this inside the subfolder loop.
Displacement=[x,y,z];
matfile = fullfile(mainfolder,subfolders(i).name,'output.mat');
save(matfile,'Displacement')
AUWAL ABUBAKAR
AUWAL ABUBAKAR 2020 年 1 月 30 日
Thanks a million. it woked perfectly.
Wasssalam

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by