MATLAB: How to cd to to different directories with a changing name in a for loop

cd

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

Best Answer

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);