import filelist into an array MATLAB

97 Views Asked by At

I have created a file list in a .txt file by using UNIX command. I need to do something on MATLAB as a loop by referring this file list. First I was stuck with importing filelist.txt into an array. My filelist.txt contains:

aa121001/121001ABC/1210010000/aa1.txt
aa121001/121001ABC/1210010000/aa2.txt
aa121001/121001ABC/1210010009/aa1.txt
aa121001/121001ABC/1210010009/aa2.txt
aa121001/121001ABC/1210010016/aa1.txt
aa121001/121001ABC/1210010016/aa2.txt
aa121001/121001ABC/1210010024/aa1.txt
aa121001/121001ABC/1210010030/aa1.txt
aa121001/121001ABC/1210010030/aa2.txt
...

In each aa1.txt or aa2.txt file, there are only numbers and no char. If I can import these strings into an array List, I want to code my program list this:

for k = 1:length(List)
myFolder = '/Users/LILI1234/Documents/DataSet';
file = fullfile(myFolder,List(k));
data = dlmread(file);
%do something 
end

So far it works for only one file (one line from the filelist.txt), but there are a lot of data files in different directories, so I want to do as a loop. Please let me know if you have any idea to help me. Thanks a lot!

1

There are 1 best solutions below

0
On

You can import the file using List=importdata('filelist.txt') then the whole list will be in List cell array. From then you can call each file by List{k} like this

myFolder = '/Users/LILI1234/Documents/DataSet'; 
for k = 1:length(List)
    file = fullfile(myFolder,List{k}); 
    data = dlmread(file); 
    %do something
end