フィルターのクリア

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 Center および File ExchangeSearch Path についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by