Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 25 Dez. 2021
Do not change directory just to import/export data files.
Use absolute/relative filepaths instead, it is more efficient and easier to debug.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Dez. 2021
Bearbeitet: Stephen23 am 25 Dez. 2021
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 Kommentare
Satyajit Ghosh
Satyajit Ghosh am 25 Dez. 2021
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 am 25 Dez. 2021
Sorry. There is a clear command in the Slave_1 script. Problem is resolved. Thank you very much @Stephen

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by