I want to write code in matlab to access files inside folders
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
t
am 15 Jun. 2023
Kommentiert: t
am 15 Jun. 2023
the code I want to write is need two for loop, one to access folders inside one folder, and the second for loop to access the files inside those folders. the folder name is RO_ISR2 and type of files are mat.I use MATLAB R2022b
this is the code which I wrote it:
Folder = 'C:\Users\97152\Downloads\copy code\RO_ISR2 (2)\RO_ISR2';
FileList = dir((Folder));
FileList = FileList(3:end);
for k = 1:numel(FileList)
folder=FileList(k).name;
folderpath=fullfile(Folder,folder);
matfiles=dir(fullfile(folderpath,'*.mat'));
p=dir('*.mat');
for j=1:numel(p)
data=load(p(j).name);
File = fullfile(FileList(k).folder, FileList(k).name);
Data{k} = readtable(File);
end
1 Kommentar
Stephen23
am 15 Jun. 2023
Bearbeitet: Stephen23
am 15 Jun. 2023
Note that this code
FileList = FileList(3:end);
is a buggy attempt to remove dot directory names:
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.)"
The recommended approaches are ISMEMBER or SETDIFF or similar.
Akzeptierte Antwort
Shaik mohammed ghouse basha
am 15 Jun. 2023
I understand you have a Main Folder and there are sub folders in it. Each sub folder has files in it and you want one loop to iterate through sub folders and other loop to iterate files in each sub folder.
I have tried to replicate your code in my system and it works fine. Here is the code:
Folder = 'C:\Users\gshaik\Downloads\copy code\RO_ISR2 (2)\RO_ISR2';
cd(Folder); %cd into the main folder
Folders_List = dir(Folder); %get List of all sub folders
Folders_List = Folders_List(3:end); % Removed the initial . ..
for i = 1:numel(Folders_List) %Iterate through all sub folders
Folder_Inside = Folders_List(i).name; %get name of each sub folder
Folder_Inside_Path = fullfile(Folder, Folder_Inside) %get full path of sub folder
mFiles=dir(fullfile(Folder_Inside_Path,'*.m')) %get all .m files in the sub folder
cd(Folder_Inside_Path); %cd into the sub folder in order to access the files in second loop
for j = 1:numel(mFiles) %iterate through all the .m files
data = load(mFiles(j).name); %perofrom any operation on the .m you want
end
end
Here I used .m files to test this code you can .mat or any type of files you want to use.
One key thing is before iterating into the .mat files in each sub folder it is important to cd into the sub folder in order to access files of sub folder hence I used the command: cd(Folder_Inside_Path);
3 Kommentare
Stephen23
am 15 Jun. 2023
Bearbeitet: Stephen23
am 15 Jun. 2023
"% Removed the initial . .. "
No, that is actually a common yet buggy attempt to remove the dot directory names. For more information:
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.)"
Avoid calling CD inside code like that (it is slow and makes debugging harder). It is much better to use absolute/relative filenames. All MATLAB functions which import/export file data accept absolute/relative filenames.
Rather than nested loops a simpler approach is to simply use DIR with appropriate wildcards.
Note that .m files are code, not data that can be LOADed.
So simpler, more robust code which fixes those bugs would be e.g.:
P = 'C:\Users\gshaik\Downloads\copy code\RO_ISR2 (2)\RO_ISR2';
S = dir(fullfile(P,'*','*.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = load(F);
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!