Read different folders by changing path with for loop

조회 수: 11 (최근 30일)
Alberto Acri
Alberto Acri 2022년 11월 6일
댓글: Stephen23 2022년 11월 7일
Hi. I have a code that takes as reference the folder 'DATA 1' containing some .jpg images:
P = 'C:\Users\Alberto\Desktop\GLOBAL\DATA1'; % folder DATA 1
However, inside the folder 'GLOBAL' there are multiple folders: 'DATA1', DATA 2', etc., and some folders may be missing, for example 'DATA 3' (but have the folder 'DATA 4').
I want to automatically apply my code so that, for each for loop, it can refer to a different folder, for example:
P = 'C:\Users\Alberto\Desktop\GLOBAL\DATA2'; % folder DATA 2
P = 'C:\Users\Alberto\Desktop\GLOBAL\DATA3'; % folder DATA 3
and so on...

채택된 답변

Stephen23
Stephen23 2022년 11월 7일
편집: Stephen23 2022년 11월 7일
P = 'C:\Users\Alberto\Download\...\...\...';
D = dir(fullfile(P,'DATA*'));
for k = 1:numel(D)
F = fullfile(D(k).folder,D(k).name);
... do whatever you want with folder F
end
  댓글 수: 2
Alberto Acri
Alberto Acri 2022년 11월 7일
Thank you for the clarification @Stephen23.
I am modifying the initial question because from the answers you gave me I realized that I did not explain myself well. I will also repost the question here.
I have a code that takes as reference the folder 'DATA 1' containing some .jpg images:
P = 'C:\Users\Alberto\Desktop\GLOBAL\DATA1'; % folder DATA 1
However, inside the folder 'GLOBAL' there are multiple folders: 'DATA1', DATA 2', etc., and some folders may be missing, for example 'DATA 3' (but have the folder 'DATA 4').
I want to automatically apply my code so that, for each for loop, it can refer to a different folder, for example:
P = 'C:\Users\Alberto\Desktop\GLOBAL\DATA2'; % folder DATA 2
P = 'C:\Users\Alberto\Desktop\GLOBAL\DATA3'; % folder DATA 3
and so on...
I hope that was clearer :)
Stephen23
Stephen23 2022년 11월 7일
"I want to automatically apply my code so that, for each for loop, it can refer to a different folder..."
Which is what my code does, using DIR to get a list of all of the foldernames.

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

추가 답변 (1개)

Khushboo
Khushboo 2022년 11월 7일
Hello,
You can refer to the following script to do this. I am basically looping through all the DATA X folders and then looping through all the folders within each DATA X (such that X=1,2,3,..):
D = ''; % specify the path to the main directory (one folder before DATA X)
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % list the subfolders of D (lists different DATA X folders where X = 1,2, and so on)
% loop through all the DATA X folders
for i = 1:numel(N)
T = dir(fullfile(D,N{i},'*')); % get all the subfolders in the current directory (DATA X)
T = T(3:end); % to exclude the '.' and '..' directories created
% rename the files within the DATA X folder
for fileNo = 1:length(T)
currFile = T(fileNo);
oldname = fullfile(currFile.folder,currFile.name);
newname = [fullfile(currFile.folder,'CorrectedExample') currFile.name(end)];
movefile(oldname,newname);
end
end
Hope this helps!
  댓글 수: 2
Stephen23
Stephen23 2022년 11월 7일
T = T(3:end); % !!! Buggy code !!!
This topic has been discussed extensively on this forum:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
Khushboo
Khushboo 2022년 11월 7일
Oh okay, thankyou for pointing it out. The code seemed to work in my case (MacOS) but yeah it will not be correct when the order in which directories are returned is different.
Thankyou for pointing it out!

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by