Want to iterate over subfolders within folder

I have a current working director or folder in MATLAB and wrote a function in this directory. Within this directory, I have a folder that contains 10 subfolders, each of which contains two different types of files (one is .edf and one is .xlsx). My function is designed to take an edf file and the corresponding xlsx file and compute something based on these.
So, my goal is to find a way that I can loop through these 10 subfolders and call my function on the edf and xlsx files in each subfolder one by one. Is there any way I can do this? Thanks.

4 个评论

A follow up question would be, what if these subfolders are all on a dropbox link and there are more than 10 of them (far too large too download them all to my computer). Is there any way I can access these subfolders in MATLAB to use my function on them iteratively? Thanks.
Please take a look at the documentation for the dir command. You will be able to do what you wanted using this comand. If it is not clear to you how to adapt this to your situation, please explain at what point you are stuck
@Jon, thanks for your response. I looked at the dir command and tried using this. I am just not sure how to iterate through folders in dir to call the function on each of these subfolders. The documentation for dir did not have any examples of how to do this.
The point is that nothing in your question actually requires itterating through folders, only through file pairs. And dir can generate that list of files for you.

请先登录,再进行评论。

 采纳的回答

Files=dir('**/*.xlsx') will give you a list of all the .xlsx files in the folder and sub-folder.
Observe Files(1), Files(2), ...
Use a for-loop to go through all the files in your program, use fileparts() to get the folder name. Use fullfile() to get the full path of the .edf file if needed.

7 个评论

I have a folder titled 'Data' in my working directory. So, I write,
temp = dir('Data');
Now, temp is
temp =
12×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
This is because my 'Data' folder which I titled 'temp' has 9 subfolders (folder(1), folder(2), folder(3) are not the subfolders I want, not sure what these are but they seem to be nonsense, but my actual subfolders are at folder(4) thru folder(12)). Now, I want to iterate through these 9 subfolders and call a function I wrote on two files (there are 1 edf file and 1 excel file in each subfolder that I need plus some other files that I don't need) from each subfolder. How can I do this?
The reason I cannot just gather all Excel files is that all of the Excel files in my subfolders have the same exact name even though they are different files.
And what happens if you don't ignore this advice? The will take care of the folders.
In MATLAB Command window, under your 'Data' folder, run below two lines and you will see
Files=dir('**/*.xlsx');
Files.folder
@Fangjun Jiang I tried this, but this just gives me the file paths of all of my Excel files, each of which is in a different subfolder. For example, when I try to read one of these excel files,
I tried
Files = dir('**/*.xlsx');
A = xlsread(Files(1));
Then it produces the following error,
Error using xlsread
File name must be a string or character vector.
Error in untitled (line 19)
A = xlsread(Files(1));
xlsread(fullfile(Files(1).folder, Files(1).name))

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Software Development Tools 的更多信息

产品

版本

R2022a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by