Open and process files in subfolders

Hi everyone! i wrote a script to process a lot of files within a folder. Now, this folder is just a subfolder in the Input-folder and i would like to apply the script to all subfolders (all files within each subfolder). i've come so far:made a list of subfolders after selecting the input folder, but don't know how to get through the subfolders and files within them. can someone help? thanks
d=uigetdir('','Select Input-folder'); %select the input-folder that contains the subfolders
cd(d);
list = dir;
list = list([list.isdir]);
list = list(~ismember({list.name},{'.' '..'}));
l=length(list);
for i=1:l
???? %open subfolder and apply script to files within it.
end

답변 (1개)

CS Researcher
CS Researcher 2016년 5월 9일
편집: CS Researcher 2016년 5월 9일

1 개 추천

Why not do the same as you did above. Inside the for loop do this:
d=uigetdir('','Select Input-folder'); %select the input-folder that contains the subfolders
cd(d);
list = dir;
list = list([list.isdir]);
list = list(~ismember({list.name},{'.' '..'}));
l=length(list);
for i=1:l
oldfolder = cd(list(i).name);
% Perform your operation on the files, e.g., if you are working with csv files
files = dir('*.csv');
numberOfFiles = length(files);
for k = 1:numberOfFiles
% You code goes here
end
cd(oldfolder);
end

댓글 수: 3

thanks a lot for your answer. the procedure seem to work. in the loop k=1:numberOfFiles i work with dat-files and i want to open and read each of them. unfortunately, i get an error because the file couldn't be open (i guess missing slash at the end of path-string provided with pwd.)
oldfolder = cd(list(i).name);
path=pwd;
fileID = fopen([path liste(lauf).name], 'r');
startRow=2;
formatSpec='%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s';
data{gesamtIndex}=textscan(fileID, formatSpec, 'HeaderLines', startRow);
fclose(fileID);
can you help me here as well?
CS Researcher
CS Researcher 2016년 5월 9일
Can you share your code with the error message?
the code is the one above and the error message:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in aufruf (line 32)
data{gesamtIndex}=textscan(fileID, formatSpec, 'HeaderLines', startRow);

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

카테고리

도움말 센터File Exchange에서 Search Path에 대해 자세히 알아보기

질문:

2016년 5월 9일

댓글:

2016년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by