MATLAB Answers

Automatically take folder directories of one level up

조회 수: 52(최근 30일)
Muhammad Abir
Muhammad Abir 2020년 11월 13일
댓글: Muhammad Abir 2020년 11월 13일
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

채택된 답변

Image Analyst
Image Analyst 2020년 11월 13일
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);
Do not use addpath() ! !

추가 답변(1개)

Jon 2020년 11월 13일
편집: Jon 2020년 11월 13일
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
Muhammad Abir
Muhammad Abir 2020년 11월 13일
Actually the user will select the folder using the following code
ImageFolder = uigetdir('D:\', 'Select Image Directory');
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

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

Community Treasure Hunt

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

Start Hunting!

Translated by