MATLAB: How to import multiple files from a folder in MATLAB

dirfilesfolders

I want to open all the files with a certain attribute (e.g. they all have 'History' in their name) and open them, one by one, in MATLAB. I know of the command dir my_name.txt which will list it like I want, but how do I now open the files that it listed?

Best Answer

Remember to fclose(fid) after you do the fscanf()
The code you show will already only locate file names ending in .txt .
You should be more careful with directories, though:
testfiledir = 'E:\MachineLearning\TestFiles\';
matfiles = dir(fullfile(testfiledir, '*.txt'));
nfiles = length(matfiles);
data = cell(nfiles);
for i = 1 : nfiles
fid = fopen( fullfile(testfiledir, matfiles(i).name) );
data{i} = fscanf(fid,'%c');
fclose(fid);
end