Dynamically change folders within a folder using MATLAB

13 views (last 30 days)
Hi,
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';

Accepted Answer

Geoff Hayes
Geoff Hayes on 5 Jan 2016
Edited: Geoff Hayes on 5 Jan 2016
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
end
end
You may need to add some additional logic to ensure that directories or folders such as '.' and '..' are ignored.
  5 Comments
Geoff Hayes
Geoff Hayes on 6 Jan 2016
Damith - if you put a breakpoint at the line
filename=[csvFiles(1).name(1:6),'.csv']
you will notice that filename is just the name of the file. It does not include the path to this file so this is probably why fileID is zero (or less than zero): the full path to the file has not been supplied so the file cannot be opened. Try doing the following instead
filename=fullfile(myFolder,[csvFiles(1).name(1:6),'.csv']);
fileID = fopen(filename,'w');
% etc.
Use the debugger to step through the code to see what is happening.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Jan 2016
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;
end
% 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
return;
end
% 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)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
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:
baseFileNames(f).name
end
else
fprintf(' Folder %s has no m-files in it.\n', thisFolder);
end
end
% Finish up at the top level folder where we started.
cd(topLevelFolder);
  4 Comments
Image Analyst
Image Analyst on 6 Jan 2016
Daminth, I found out we do need to call eval() but we need to strip off the .m extension first:
% Strip off the .m extension.
[~, baseName, ext] = fileparts(baseFileNames(f).name);
% Here is where we actually run the m-file:
eval(baseName)
Here is the full code. This will run every m-file in the selected folder and all subfolders.
% 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;
end
% 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
return;
end
% 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)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
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);
% Strip off the .m extension.
[~, baseName, ext] = fileparts(baseFileNames(f).name);
% Here is where we actually run the m-file:
eval(baseName)
end
else
fprintf(' Folder %s has no m-files in it.\n', thisFolder);
end
end
% Finish up at the top level folder where we started.
cd(topLevelFolder);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!