MATLAB Answers

Automatically take folder directories of one level up

14 views (last 30 days)
I have 3 folders of images. Now I want to enter into one folder and select that directory. Then I want matlab to go back to one level up and automatically select other two folder's directory. Is there any easirer way to do that?
Thanks in advance

Accepted Answer

Image Analyst
Image Analyst on 13 Nov 2020
Try this:
[parentFolder, thisFolder] = fileparts(ImageFolder)
fileListing = dir(parentFolder) % List of everything: folders and files:
% Get a list of only the folders
areAFolder = [fileListing.isdir];
% Extract only those items that are folders, not files;
folderListing = fileListing(areAFolder)
% Just for fun, list all the folders that we found:
fprintf('Found these %d child folders of "%s" :\n', length(folderListing), parentFolder)
for k = 1 : length(folderListing)
thisFolder = fullfile(folderListing(k).folder, folderListing(k).name);
fprintf('%s\n', thisFolder);
end
Do not use addpath() ! !

More Answers (1)

Jon
Jon on 13 Nov 2020
Edited: Jon on 13 Nov 2020
You can specify the full path to the directories you want to open rather than changing the directory that MATLAB is running in. So keep MATLAB in the same directory and only change the path e.g. 'myDir1/mysubDir2/myfileA.ext' back of 'myDir1/myfileB.ext' to the file you want to open
  3 Comments
Muhammad Abir
Muhammad Abir on 13 Nov 2020
Actually the user will select the folder using the following code
ImageFolder = uigetdir('D:\', 'Select Image Directory');
addpath(ImageFolder);
files= dir(ImageFolder);
Once selected, matlab should get images from direcotry one level before. Let's say if user selects C:\Imagedata\Image, then if there is more folder in C:\Imagedata directory, like C:\Imagedata\gain, then once user selects C:\Imagedata\Image directory, Matlab should be able to get C:\Imagedata\gain directory automatically

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!