how to access images from sub folders?

hi. i have a folder F1 and then in this folder i have S1 and S2 afterwards in each S1 and S2 i have E1 and E2 finally in these folders i have images named as frame_0000.jpeg till frame_2000.jpeg. for better understanding i am attaching a pic of my hierarchy.
after accessing each image i have to convert them from rgb2gray, apply my required preprocessing and then make excel file of each images in 'E' folders. Please help me accessing these images. waiting for your kind response.

 Accepted Answer

Stephen23
Stephen23 on 29 Sep 2018
Edited: Stephen23 on 29 Sep 2018
Using dir:
D = 'path to directory F1';
S = dir(fullfile(D,'S*'));
for ii = 1:numel(S)
E = dir(fullfile(D,S(ii).name,'E*'));
for jj = 1:numel(E)
F = dir(fullfile(D,S(ii).name,E(jj).name,'frame*.jpg'));
for kk = 1:numel(F)
N = fullfile(D,S(ii).name,E(jj).name,F(kk).name);
im = imread(N);
end
end
end
Using cell arrays and sprintf:
F = 'path to directory F1';
S = {'S1','S2'};
E = {'E1','E2'};
V = 0:2000;
for ii = 1:numel(S)
for jj = 1:numel(E)
for kk = 1:numel(F)
N = fullfile(D,S{ii},E{jj},sprintf('frame_%04d.jpg',V(kk)));
im = imread(N);
end
end
end
See also:

10 Comments

Thanku sooooooooooooooooooooooooooooooo much. in my case dir is working. but i have to embed my algo in it. lets see what happens... Thanks again.
sidra Rafique's "Answer" moved here:
hi, Is there any way of making separate sheet for all subfolders? I have created an excel sheet but all the images from subfolders (E1, E2) are in one column.(i.e; 0,1,2,3,0,1,2,3..). i want to write frames of E1 in Sheet1 and E2 in sheet2. waiting for your responce.
If you are using xlswrite then you can specify its third input as the sheet name:
xlswrite(filename,matrix,E(jj).name,...)
This will give you sheets E1 and E2. Use strrep if you really want the sheets named Sheet1 and Sheet2:
strrep(E(jj).name,'E','Sheet')
i am getting this error:
Index exceeds matrix dimensions.
Error in YeLo (line 97)
xlswrite('Diameters', Whole_Data,E(xx).name);
where diameter is my xls file. whole_Data is matris:
Data = {xx, d};
Whole_Data = vertcat(Whole_Data, Data);
but beside this my output xls file is showing two sheets of e1 and e2 but with only 2 entries. infact i have 6 frames in each e1 and e2.
error is removed. its working but with slight problem. in xlsx file e1(sheet1) is writing correct frames(i.e; 6) but in e2(sheet2) its showing 12 frames(of both e1 and e2).but it has to show its own folder frames only. how to resolve it. Need your help.
@sidra Rafique: perhaps the problem occurs with the index xx:
E(xx).name
You need to check that the index you are using is correct for that variable. I have no idea how you defined xx, so you have to check that yourself: does it iterate over the number of elements in E ?
"beside this my output xls file is showing two sheets of e1 and e2 but with only 2 entries. infact i have 6 frames in each e1 and e2."
If you want to write multiple frames per sheet then you could:
  • concatenate the frames before writing them at once to a sheet.
  • write each frame individually, writing to different parts of the sheet by specifying the address (optional input to xlswrite)
  • as an alternative, write to a different sheet for each frame (e.g. using the frame name/number as the sheet name.
sorry. i am not getting your point. let me explain. acctually i want to make 2 excel sheets for S1 folder and S2 folder. and in each excel sheet i want 2 sheets E1 and E2. which means frames of E1 and E2 subfolder will be in a separate excel sheet which is relavent to S1 data. and similarly frames of E1 and E2 subfolder will be in a separate excel sheet which is relavent to S2 data. as shown in the tree above. right now i am getting is:
  • e1 and e2 sheets for both s1 and s2 in a single excel file.(which is not my requirement)
  • e1 is showing more frames than present in subfolders and e2 is showing frames of its own subfolder as well as e1 subfolder.(which is as you said because of the wrong iterator)
but i am unable to track my error. need your help.
Stephen23
Stephen23 on 30 Sep 2018
Edited: Stephen23 on 30 Sep 2018
"...i want to make 2 excel sheets for S1 folder and S2 folder. and in each excel sheet i want 2 sheets E1 and E2..."
What does this mean? As far as I know, Excel worksheets cannot contain other worksheets. An excel worksheet can contain data (numeric, text) and formulas... and some other kinds of objects. But not worksheets, as far as I am aware.
Perhaps you mean that you want a different workbook (i.e. Excel file) for each S1 and S2 folders? And within each workbook you want two worksheets (i.e. sheets) corresponding to the E1 and E2 folders?
yes. i mean workbook and worksheets. you are right.
Stephen23
Stephen23 on 1 Oct 2018
Edited: Stephen23 on 1 Oct 2018

@sidra Rafique: it seems that you want to use the S loop to define the filename, and the E loop to define the sheet name, something like this:

D = 'path to directory F1';
S = dir(fullfile(D,'S*'));
for ii = 1:numel(S)
    fnm = sprintf('%s.xlsx',matrix,S(ii).name);
    E = dir(fullfile(D,S(ii).name,'E*'));
    for jj = 1:numel(E)
        sht = E(jj).name;
        F = dir(fullfile(D,S(ii).name,E(jj).name,'frame*.jpg'));
        out = cell(1,numel(F))
        for kk = 1:numel(F)
            N = fullfile(D,S{ii},E{jj},sprintf('frame_%04d.jpg',V(kk)));
            im = imread(N);
            out{kk} = ... whatever data you want to save
        end
        mat = vertcat(out{:});
        xlswrite(fnm,mat,sht,...)
    end
end

Sign in to comment.

More Answers (1)

KSSV
KSSV on 14 Sep 2018
A simple google will give you lot of links to this question. This question is discussed and asked lot many times.

4 Comments

but i want to access these images using switch-case statements. i have tried to access directory using switch-case but i dont know where i am wrong. i am constantly getting wrong+infinite loop output.
if true
fCount=0;sCount=0;eCount=0;
while(fCount==0)
switch(sCount)
case 0
Directoryofsub = 'E:\f1\sub1';
switch(eCount)
case 0
Imgs = sprintf('E:/f1/s1/e1/');
eCount=1;
case 1
Imgs = sprintf('E:/f1/s1/e2/');
eCount=2;
end
sCount=1;
case 1
Directoryofsub = 'E:\f1\s2';
switch(eCount)
case 0
Imgs = sprintf('E:/f1/s2/e1/');
eCount=1;
case 1
Imgs = sprintf('E:/f1/s2/e2/');
eCount=2;
end
otherwise
fCount=1;
sCount=2;
fprintf('Thank you for using me');
end
end
end
this code is only accessing e1 of case 0 and e2 of case 1 and then keep on repeating e2 of case 1 infinite times. please guide me where is the error. waiting for your response.
Stephen23
Stephen23 on 29 Sep 2018
Edited: Stephen23 on 29 Sep 2018
"but i want to access these images using switch-case statements."
Why? This seems a very complex approach. A simple dir call, like KSSV's link shows, would do the job perfectly (unless there are some other requirements that you did not mention in your question).
By using KSSV's approach i can get count of the images and that count cannot be used as argument in 'imread' command.(i am hoping you have read KSSV's prescribed code). i have to read each image using 'imread' and later i have to make a excel sheet of name of the each folder and files and their corresponding values. but if you have any other suggestion then please do share it with me or make changes in my switch statements. So that i can work further as i am stuck in this for last 4 weeks. thankyou
Looks like this doesn't apply anymore since you accepted the other answer.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 14 Sep 2018

Edited:

on 1 Oct 2018

Community Treasure Hunt

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

Start Hunting!