# How to import a sequence of files into a cell in matlab?

I'm working on class assignment. I've to construct a hidden markov model for a give sequence of discrete data. The data is a 2-dimensional vector separated by comma in which each line represents x,y coordinates. I've been given 500 files each for 3 classes.

The name file looks like this "class.txt" so if the file belongs to class1 and it is 2nd example and has a sequence length of 42 then the file name look like this "class1_2_42.txt"

My question how to import data from those files into a cell or matrix of size (500X2).

I tried following this code from matlab help

For example, to read files named file1.txt through file20.txt with importdata:

numfiles = 20;
mydata = cell(1, numfiles);

for k = 1:numfiles
myfilename = sprintf('file%d.txt', k);
mydata{k} = importdata(myfilename);
end


But I can't work it out because of the name of the given files. Please help

For reference I added the images of how my files and data look like

## 1 Answer

The code is self explicable.

res = dir('*.txt'); %to read all files with *.txt
nfiles = length(res);
mydata = cell(1, nfiles);
for k = 1:nfiles
mydata{k} = importdata(res(k).name);
end
`