Info

This question is closed. Reopen it to edit or answer.

Error using cd Cannot CD to 7364 (Name is nonexistent or not a directory).

1 view (last 30 days)
I have these two chunks of code. The first one serves to open a still image from each video file and ask me to select a region of interest using a polygon. That step works just fine for all videos (the structure is one large folder with subfolders for each video). Then the next step asks me to measure the arm by clicking two points. That works for the first video, but when the for loop restarts I get an error.
for i = 1:length(nameFolds)
cd(cell2mat(nameFolds(i)));
ToDoList = dir('*.mp4');
ToDo = {ToDoList.name}';
mov = VideoReader(char(ToDo));
imag = read(mov, 60);
imshow(imag);
title(nameFolds(i));
disp('Please draw the boundaries of one of the OPEN arms');
Opens(i,:,:) = roipoly;
disp('Now please draw the boundaries of the other OPEN arm');
Opens(i,:,:) = squeeze(Opens(i,:,:)) + roipoly;
cd(home);
end
if (ArmMeasuring == 1)
for i = 1:length(nameFolds)
cd(cell2mat(nameFolds(i)));
ToDoList = dir('*.mp4');
ToDo = {ToDoList.name}';
mov = VideoReader(char(ToDo));
imag = read(mov, 60);
imshow(imag);
title(nameFolds(i));
disp('Please click two points measuring the length of a single arm');
[x,y] = ginput(2);
ArmLengths(i) = sqrt( (x(1)-x(2))^2 + (y(1)-y(2))^2);
end
end
The error is "Error using cd
Cannot CD to 7364 (Name is nonexistent or not a directory).
Error in EPMCompiler (line 60)
cd(cell2mat(nameFolds(i)));"
I can see that the subfolder entitled 7364 exists and is in the same location as the first subfolder that works fine. Further more, the program is able to find the 7364 folder just fine during the first chunk of code. This is very confusing to me and I'm not sure what's going on.
  2 Comments
Stephen23
Stephen23 on 11 Sep 2020
"This is very confusing to me and I'm not sure what's going on."
cd is the problem. You can find it in substandard code like this to read files from different directories, whereas the better** approach is to NOT change the directory and simply use absolute/relative filenames:
** better in the sense more robust, more effiicient, easier to debug.
Hannah Harder
Hannah Harder on 11 Sep 2020
Well I think the point of using cd is to make this code generalizable. This serves to let the same code work on any folder of videos. Can I use relative file names in the same way?

Answers (4)

John Petersen
John Petersen on 10 Sep 2020
You are changing directories in the first for loop so that by the time you get in to the second for loop you are not starting in the same spot so cd doesn't see the directory.
You need to do something like this:
CurrDir = pwd;
for ...
end
cd (CurrDir)
if (ArmMeasuring == 1)
for ...
end
end
  2 Comments
Hannah Harder
Hannah Harder on 11 Sep 2020
I did not write this code, so I can't comment on why this is. But I know that this code worked previously. Is there any chance this is due to something on my specific computer and not something that needs changed in the code?

Image Analyst
Image Analyst on 11 Sep 2020
  1 Comment
Hannah Harder
Hannah Harder on 11 Sep 2020
I did not write this code, so I didn't know about this issue. Is the syntax for fullfile() the same as for cd()? I read the FAQ but am very confused. I am new to Matlab and am not a code expert by any means.

Image Analyst
Image Analyst on 11 Sep 2020
Get rid of all calls to cd and get the full file names like this:
topLevelFolder = 'D:\My Videos' % Wherever you want.
filePattern = fullfile(topLevelFolder, '**\*.mp4')
fileNames = dir(filePattern);
for k = 1:length(fileNames)
thisFileName = fullfile(fileNames(k).folder, fileNames(k).name);
fprintf('Processing %s (#%d of %d)...\n', thisFileName, k, length(fileNames));
videoObject = VideoReader(thisFileName);
rgbImage = read(videoObject, 60);
imshow(rgbImage);
title(thisFileName, 'Interpreter', 'none');
promptMessage = sprintf('Please draw the boundaries of one of the OPEN arms');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return;
end
% Opens(k,:,:) = roipoly;
promptMessage = sprintf('Now please draw the boundaries of the other OPEN arm');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return;
end
% Opens(k,:,:) = squeeze(Opens(k,:,:)) + roipoly;
delete(videoObject);
end

Hannah Harder
Hannah Harder on 11 Sep 2020
I'll be honest everybody - I tried to follow your suggestions but it was too complicated for me. I just inserted another cd(home) call into the second for loop and that worked! I get that in an ideal world, I wouldn't use cd. But the code was written by someone with a lot more experience than me, and I do not have the knowledge to change all the cds. Thanks so much for all your help and hopefully I'll eventually know enough to rewrite this code properly.
  1 Comment
Rik
Rik on 11 Sep 2020
Which answer are you replying to? Please post this comment there instead of posting it as an answer.

Community Treasure Hunt

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

Start Hunting!