필터 지우기
필터 지우기

How do I cd inside a for loop to a bunch of folders containing a file with same name?

조회 수: 2 (최근 30일)
I have some data in a directory saved in various folders with name 1_1_1, 1_1_2, .... etc. All files in those folder have same name. I have defined a for loop and in that I want to cd to required folders (1_1_1, 1_1_2,....) one by one. Hence in the pathname I need to input the loop varibales. Please see below script.
clear;clc
for a=1:1:1
for v=1:1:1
for s=1:1:10
formatSpec='/media/satyajit/My_Passport/Satyajit/Extra_Backups/folder1/folder2/%d_%d_%d';
str=sprintf(formatSpec,a,v,s);
cd str
Script_1
Script_2
end
end
end
But, it is showing error - "Error using cd
Cannot CD to /home/satyajit/str (Name is nonexistent or not a directory).
Error in Master (line 27)
cd str"
  댓글 수: 1
Stephen23
Stephen23 2021년 12월 25일
Do not change directory just to import/export data files.
Use absolute/relative filepaths instead, it is more efficient and easier to debug.

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

채택된 답변

Stephen23
Stephen23 2021년 12월 25일
편집: Stephen23 2021년 12월 25일
Do not change directory just to import/export data files.
Use absolute/relative filepaths instead, it is more efficient and easier to debug:
P = '/media/satyajit/My_Passport/Satyajit/Extra_Backups/folder1/folder2';
for a=1:1:1
for v=1:1:1
for s=1:1:10
T = sprintf('%d_%d_%d',a,v,s);
F = fullfile(P,T)
... use the filepath F here
end
end
end
Note that you can even call scripts in any folder using RUN.
  댓글 수: 6
Satyajit Ghosh
Satyajit Ghosh 2021년 12월 25일
Slave_1 is a script. It is not recognizing the F,P,T variables. Error -
"Undefined function or variable 'P'.
Error in Slave_1 (line 10)
a1=load(fullfile(P,T,'Singles_S1.txt'));
Error in Master (line 32)
Slave_1"
Satyajit Ghosh
Satyajit Ghosh 2021년 12월 25일
Sorry. There is a clear command in the Slave_1 script. Problem is resolved. Thank you very much @Stephen

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by