Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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 commentaire
Stephen23
Stephen23 le 25 Déc 2021
Do not change directory just to import/export data files.
Use absolute/relative filepaths instead, it is more efficient and easier to debug.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 25 Déc 2021
Modifié(e) : Stephen23 le 25 Déc 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 commentaires
Satyajit Ghosh
Satyajit Ghosh le 25 Déc 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 le 25 Déc 2021
Sorry. There is a clear command in the Slave_1 script. Problem is resolved. Thank you very much @Stephen

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Tags

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by