Filter löschen
Filter löschen

Read different folders by changing path with for loop

18 Ansichten (letzte 30 Tage)
Alberto Acri
Alberto Acri am 6 Nov. 2022
Kommentiert: Stephen23 am 7 Nov. 2022
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...

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Nov. 2022
Bearbeitet: Stephen23 am 7 Nov. 2022
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 Kommentare
Alberto Acri
Alberto Acri am 7 Nov. 2022
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 am 7 Nov. 2022
"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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Khushboo
Khushboo am 7 Nov. 2022
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 Kommentare
Stephen23
Stephen23 am 7 Nov. 2022
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 am 7 Nov. 2022
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by