How do I write a dynamic search path?

11 views (last 30 days)
Katherine
Katherine on 24 Jul 2014
Answered: Geoff Hayes on 24 Jul 2014
I'm working on a parsing file that will loop through several levels of folders to read a .csv file from within the lowest level. I am trying to write a dynamic search path to do this so that the script will rewrite the directory that I am searching for the .csv files in every time. So far my code to reset the search path and store the new search path in the directory variable looks something like this:
for j = 1:length(directory)
datapathf1 = 'C:\Users\puffypuffin\Desktop\DATA\j';
directory = dir(strcat(datapathf1,'\f130'))
end
I want j to represent the folder level in which there are several different folders, i.e. f130, c220, and t500. However, I don't believe that my code is doing this correctly. Is there a syntax for having a variable in a dynamic searchpath? Thanks!

Answers (1)

Geoff Hayes
Geoff Hayes on 24 Jul 2014
Katherine - in your code, what is directory? Presumably, it is an array with one or more files and folders, but from the way that you are using it, it is as if there are just directories numbered 1,2,3,etc.
for j = 1:length(directory)
datapathf1 = 'C:\Users\puffypuffin\Desktop\DATA\j';
If that is the case, then you need to employ the sprintf function to set the integer directory in the datapathf1 given j
for j = 1:length(directory)
datapathf1 = sprintf('C:\Users\puffypuffin\Desktop\DATA\%d',j);
Since j is a level (is this a level deep or ?) then you rather than using the length of directory (which gets overwritten at the second line of the for block!) you could do something like
maxLevels = 42; % assumes 42 levels
for k=1:maxLevels
datapathf1 = sprintf('C:\Users\puffypuffin\Desktop\DATA\%d',k);
end
I'm using k in the above to avoid confusion with the MATLAB i and j representation for the imaginary number.
Now each level has several directories beneath it: f130, c220, and t500. Is it just these three, or are there more? And once you build that new folder (with f130 say) what do you do next? Get all files and folders beneath it to look for a specific file?
If you are you trying to build a list of all directories beneath the current one, then you could write a recursive function to do that
function [folders] = getFolders(currentFolder)
folders = {currentFolder};
dirData = dir(currentFolder);
for k=1:length(dirData)
if dirData(k).isdir && strcmpi(dirData(k).name,'.')~=1 ...
&& strcmpi(dirData(k).name,'..')~=1
newFolderToQuery = fullfile(currentFolder,dirData(k).name);
childFolders = getFolders(newFolderToQuery);
folders = [folders ; childFolders];
end
end
end
You could call the above function with the input parameter as 'C:\Users\puffypuffin\Desktop\DATA'. That would build all directories which you could then use later to look for a certain file. Or you could modify the above to include a second input which would be a filename (or list of files) to search for. The end result could then be to return a list of all folders that contain these files.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!