Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-9-11
"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 2020-9-11
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?

回答(4 个)

John Petersen
John Petersen 2020-9-10
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 个评论
Hannah Harder
Hannah Harder 2020-9-11
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 2020-9-11
  1 个评论
Hannah Harder
Hannah Harder 2020-9-11
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 2020-9-11
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 2020-9-11
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 个评论
Rik
Rik 2020-9-11
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!

Translated by