How can I cd to to different directories with a changing name in a for loop

2 次查看(过去 30 天)
As title
If I have the following directories, and I want to read data under these paths
./data_1/temp.txt
./data_2/temp.txt
./data_3/temp.txt
for i=1:3
cd ./data_$i
j(i)=dlmread('temp.txt');
cd ..
end
How can I make it happens?
Thank you

采纳的回答

Image Analyst
Image Analyst 2011-10-9
newFolder = sprintf('./data_%d', i);
cd(newFolder);
However, in my opinion, you're better off just constructing the full file name with fileparts() and fullfile() than changing the directory. I'd leave the directory alone and just read in the file giving dlmread the full filename (folder + base filename + extension).
fullFileName = sprintf('./data_%d/temp.txt',i);
j{i} = dlmread(fullFileName);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by