I have two sub-folders with images and require to perform operations using them. When i am running the code with sub-folder in the current folder window its being executed but i need to extract them from the main folder and run them. Also the ouput is a text file, which needs to be present in respective sub-folders instead of the current folder window. Please help me with these issues. Thank you in advance.

 채택된 답변

Bob Thompson
Bob Thompson 2021년 2월 25일

0 개 추천

I recommend using dir to capture all the contents of your parent folder within a variable. Then you can use the contents of that variable to specify which child folder you want to work in.
folders = dir('parent folder path'); % Captures all contents of parent folder
folders = folders([folders(:).isdir]==1); % Filter to just folders
folders = folders(3:end); % Remove unnecessary '.' and '..' directories
for i = 1:length(folders)
path = [folders(i).folder,'\',folders(i).name];
% do work with path variable directing to each child folder.
end

댓글 수: 5

Aishwarya Sanaka
Aishwarya Sanaka 2021년 2월 25일
Hi Bob, Thanks for responding. I am sorry for not clearly presenting my problem but your suggestion is already working in my code.The code is working when the images and the program are in the folder but when the program file is present in the window. Matlab is not able to read images from the folder. So i want to knwo if that problem can be solved. I have attached the screenshots so that it is better to understand.
Bob Thompson
Bob Thompson 2021년 2월 25일
Hmm, I'm not sure I understand. MATLAB is capable of finding .jpg files in any directory it's shown to. Knowning that I don't understand the problem, here are some thoughts:
  • Are you receiving a specific error message?
  • Are you capturing the files within the subfolders, not just the contents of the parent folder?
Aishwarya Sanaka
Aishwarya Sanaka 2021년 2월 26일
Hey Bob, I am sorry have figured that your suggestion works and the problem has been solved. Yes the files within subfolders are being captured this has been solved by using 'genpath'. My bad for the confusion. Thanks again!
Monalisa Chikezie
Monalisa Chikezie 2022년 7월 9일
Hey@Bob Thompson, I tried following your suggestion for my code and got the following error. I want
Dot indexing is not supported for variables of this type.
For me, my main folder has 70 subfolders. Each of the sub folders have 4 files.
Bob Thompson
Bob Thompson 2022년 7월 9일
Please share more of your code. From what I posted here, the error indicates that somehow 'folders' is not a structure class, but without knowing more about where the error is occurring in your work, I can't say if that's actually the problem.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 2월 25일

댓글:

2022년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by