Error using cd. Name is nonexistent or not a directory.

5 visualizaciones (últimos 30 días)
Hi. I get this error when the code starts the second time through the for loop. I want it to load the same file from each subfolder in the main folder.
pathname = uigetdir('E:\PairStim\ ', 'select dir');
cd(pathname)
f = dir(pathname);
f.isdir % to get 1 for folder and 0 for file
for i = 1: length(f)
cd(f(i).name)
preemg = dir('*Pre*DelsysAVG*');
filename = preemg.name;
preemg = load(filename);
end
Any ideas how to get the folder to change with the loop :)

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Sept. de 2013
When you use dir(), the .name field returned is always only the last component, the name within its local directory, and does not include the directories before that. You should use fullfile() and add the directory on to the name.
cd( fullfile( pathname, f(i).name ) )

Más respuestas (1)

Image Analyst
Image Analyst el 13 de Sept. de 2013
Use genpath().
  1 comentario
Image Analyst
Image Analyst el 14 de Sept. de 2013
For what it's worth, here's code to traverse into each subfolder off a main folder:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all PNG and TIF images in
% that folder and all of its subfolders.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox\images\imdemos');
% Ask user to confirm or change.
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};
fprintf('Processing folder %s\n', thisFolder);
% Get PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing image file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by