Cannot CD to (name is nonexistent or not a directory)

I am getting this error at cd(folder).
thisFile = mfilename('C:\Users\user\Documents\mu\see');
[folder,name] = fileparts(thisFile);
cd(folder)
addpath('\do');

 Accepted Answer

create_missing = false;
parts = {'C:\', 'Users', 'user', 'Documents', 'mu'};
if ~exist(parts{1}, 'dir')
error('You do not have a C: drive. Giving up.');
end
failed = false;
for K = 2 : length(parts)
thispath = fullfile(parts{1:K});
if exist(thispath, 'dir')
fprintf('Okay we already have directory %s\n', thispath);
elseif create_missing
try
mkdir(thispath);
fprintf('Was able to create missing directory %s\n', thispath);
catch
fprintf('Failed trying to create missing directory %s', thispath);
oldpath = fullfile(parts{1:K-1});
p = fileattrib(oldpath);
fprintf('Attributes of parent directory %s are:\n', oldpath);
disp(p);
failed = true;
break
end
else
fprintf('Directory %s does not exist and you asked that missing directories not be created. Set the variable create_missing to true if you want the directory created\n', thispath);
failed = true;
break
end
end
if failed
fprintf('Some directory not created. Not ready to use\n');
else
fprintf('Okay, should be ready to use directory %s\n', fullfile(parts{:}) );
end

More Answers (1)

That folder does not exist. Just because you typed it in does not mean it will exist. Call the mkdir() function to create it, but chances are you should not be using cd anyway, but creating full filenames with the fullfile() function instead. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.

Categories

Community Treasure Hunt

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

Start Hunting!