I want to write code in matlab to access files inside folders
Show older comments
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 Comment
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.
Accepted Answer
More Answers (0)
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!