error with changing directory in for loop

조회 수: 5 (최근 30일)
Vincent Davis
Vincent Davis 2019년 7월 16일
편집: Walter Roberson 2019년 7월 16일
I'm having a problem with "cd" when looping through folders using an SSH (Putty). Here is the relevant part of my function:
for ii = 1:10
parfor i=1:size(xmat,1)
file = sprintf('res_%d',i);
x0 = xmat(i,:);
[t,Y] = hackathon_ode(x0);
myFolder = cd(['output/mat_npaths_1000/np_10000_binsz_0.01/' int2str(ii) '/']);
dlmwrite(file, [t Y], ' ')
x1 = xmat1(i,:);
[t,Y] = hackathon_ode(x1);
myFolder = cd(['output/mat_npaths_1000/np_10000_binsz_0.1/' int2str(ii) '/']);
dlmwrite(file, [t Y], ' ')
And I get this as an output:
cannot CD to
/output/mat_npaths_1000/np_10000_binsz_0.1/1/output/mat_npaths_1000/np_10000_binsz_0.1/1
(Name is nonexistent or not a directory).
This is what it should look like:
/output/mat_npaths_1000/np_10000_binsz_0.1/1
When I run this from my computer it works fine. Can anyone see what I'm missing and why it repeats the directory on Putty?

채택된 답변

Adam Danz
Adam Danz 2019년 7월 16일
Do not use cd(); use fullfile() instead and then feed in the full path of the file to dlmwrite().
  댓글 수: 1
Steven Lord
Steven Lord 2019년 7월 16일
I agree with Adam Danz's suggestion. As an explanation for what's happening: you cd into a directory but then never return to the directory you were in before the cd call. Therefore when you cd a second time, you're changing to a directory relative to where you were after your first cd.
Or to use a whimsical example: you're doing the Hokey Pokey by putting your right foot in then trying to put your right foot in again. You skipped the "put your right foot out" step inbetween.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by