Dynamically change folders within a folder using MATLAB
Afficher commentaires plus anciens
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';
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 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 commentaires
Damith
le 5 Jan 2016
Image Analyst
le 5 Jan 2016
Modifié(e) : Image Analyst
le 5 Jan 2016
I think it should. Put a breakpoint on this line. That is where it actually runs it
baseFileNames(f).name
If that doesn't work, try
eval(baseFileNames(f).name);
Image Analyst
le 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);
Damith
le 15 Jan 2016
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!