필터 지우기
필터 지우기

I want to write code in matlab to access files inside folders

조회 수: 34 (최근 30일)
t
t 2023년 6월 15일
댓글: t 2023년 6월 15일
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
Stephen23
Stephen23 2023년 6월 15일
편집: Stephen23 2023년 6월 15일
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.

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

채택된 답변

Shaik mohammed ghouse basha
Shaik mohammed ghouse basha 2023년 6월 15일
Hi @hanoo,
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
Stephen23
Stephen23 2023년 6월 15일
편집: Stephen23 2023년 6월 15일
"% 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
t
t 2023년 6월 15일
I will edit the code, thank you

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by