Dynamically change folders within a folder using MATLAB

26 ビュー (過去 30 日間)
Damith 2016 年 1 月 5 日
コメント済み: Damith 2016 年 1 月 15 日
I would like to know whether I can change the folders dynamically within "streamflow_122" folder using MATLAB. I have over 150 sub-folders within "streamflow_122" folder. I have a MATLAB script to perform a task but I have change the folder by folder manually to run the script for each sub-folder.
Any ideas?
Thanks in advance.
cd ('C:\Users\Desktop\streamflow_122\')
myFolder = 'C:\Users\Desktop\streamflow_122\21601';


Geoff Hayes
Geoff Hayes 2016 年 1 月 5 日
編集済み: Geoff Hayes 2016 年 1 月 5 日
Damith - you can use the dir function to list the folder contents and iterate through over those that are directories/folders to perform the desired action. For example,
cd ('C:\Users\Desktop\streamflow_122\');
folderContents = dir;
for k=1:length(folderContents)
if folderContents(k).isdir
myFolder = fullfile(pwd, folderContents(k).name);
fprintf('%s\n',myFolder); % or perform task
You may need to add some additional logic to ensure that directories or folders such as '.' and '..' are ignored.
  5 件のコメント
Damith 2016 年 1 月 15 日


その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 1 月 5 日
You can use genpath() to generate a list of all folders within some top level folder. Then you can cd to each folder and use dir() to get the name of all m-files in that folder, and run them all (if that's what you want to do). See full demo below:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
listOfFolderNames = [listOfFolderNames singleSubFolder];
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
% Change directory to this folder so it will be the current folder and we can run the m-file.
fprintf('Processing folder %s\n', thisFolder);
% Get m files.
filePattern = sprintf('%s/*.m', thisFolder);
baseFileNames = dir(filePattern);
numberOfMFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
numberOfMFiles = length(baseFileNames);
if numberOfMFiles >= 1
% Go through all those files.
for f = 1 : numberOfMFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Running m-file %s\n', fullFileName);
% Here is where we actually run the m-file:
fprintf(' Folder %s has no m-files in it.\n', thisFolder);
% Finish up at the top level folder where we started.
  4 件のコメント
Damith 2016 年 1 月 15 日


Community Treasure Hunt

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

Start Hunting!

Translated by