I need to create a nested struct from nested folders

3 views (last 30 days)
I have some folders in the following way:
Folder 1
Folder 1.1
Folder 1.1.1
img 1.1.1.1
.
.
.
img 1.1.1.36 (between 35 to 37 imgs in each folder)
Folder 1.1.2
img 1.1.2.a
.
.
.
img 1.1.2.37 (between 35 to 37 imgs in each folder)
.
.
.
.
Folder 1.1.148098
I have to combine the img files to form videos. I am trying to create nested structures so that I can access the images (img 1.1.1.1. etc. in each folder). I have tried 'dir' but it does not create nested structures. These videos will be inputs for my ML model and I don't want to use multiple for loops if I can avoid it.
Specifically (apologies if I am providing redundant explanation), I want to do something like:
for i = 1: size(Folder1.Folder1.1.Folder1.1.1.Folder 1.1.1)
image= imread(Folder1.Folder1.1.Folder1.1.1.Folder 1.1.1.img1.1.1.i)
writevideo(v, image)
end
Any help is appreciated.

Accepted Answer

Chunru
Chunru on 17 Jun 2021
% List all the file in current folder and subfolders
fn = dir('**/*'); % structure for the information of all the file
% fn is a structure with fields:
% name: '.'
% folder: 'C:\ABCD'
% date: '01-May-2021 21:50:47'
% bytes: 0
% isdir: 1
% datenum: 7.3828e+05
% Form/sort the image filename list based on fn according to your
% requirement
% Assume fn contains the files that can be used directly
for i=1:length(fn)
if ~fn(i).isdir
current_fn = fullfile(fn(i).folder, fn(i).name);
% read file/write file
end
end

More Answers (0)

Categories

Find more on Structures 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!