MATLAB: Read different files with different names (string) in a loop in MATLAB


Hello All,
I have a folder with different .dat files in it (with different names such as a.dat, b.dat and…). I have created a cell with C={'a.dat', 'b.dat', 'c.dat', …}. Now I want to read all of those files in a For loop. For example for the i=1 in the loop, I want to read the 'a.dat", for i=2, 'b.dat', and so on. Do you have any idea how can I do that? I am trying to use something similar to fid(i) = fopen(sprintf('C(1,i)'),'rt'); but it does not work.

Best Answer

You don't need to use sprintf. Try the following:
C={'a.dat', 'b.dat', 'c.dat',...};
for i = 1:n
fid(i) = fopen(C{i},'rt')