Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 25 Dic 2021
Do not change directory just to import/export data files.
Use absolute/relative filepaths instead, it is more efficient and easier to debug.

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Search Path in Help Center e File Exchange

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by