MATLAB: Read mulitple files in matlab

while loop

This is my code. I want to find a way to (maybe a for loop or something) that make the fopen read multiple files ending on data.txt. I have 1_data.txt, 2_data.txt ect. in a folder on my computer.
data = fopen('1data.txt', 'r');
x = fgetl(fid);
ii = 0;
while ischar(x)
ii = ii + 1;
tmp = textscan(tline, '%s', 'delimiter', ' ', 'MultipleDelimsAsOne', 1);
tmp = tmp{1};
t(ii) = str2num(tmp{8});
g(ii) = str2num(tmp{9});
x = fgetl(fid);
end
fclose(fid);
scatter(g,t)

Best Answer

dinfo = dir('*.txt');
for K = 1 : length(dinfo)
thisfilename = dinfo(K).name;
%read the file
end
Read about load, importdata_, textscan to load whole text file data instead of reading line by line.