Read a list of filenames in a text file
10 views (last 30 days)
Show older comments
Help this isn't working
So i need to open a txt file with 675 rows, in each row is the filename - 675 mat files (they don't have the .mat just the name)
so to clarify theres 675 filenames in one text file, the 675 filenames are files which are in the same folder as the txt file
Each of the files have 512 rows and 32 columns
fid = fopen('...filename','r');
n = 675;
N = 512;
C = 32;
for i=1:n;
file{i}=fgetl(fid);
end
x = (file,'%g',[N,C]);
x = x';
6 Comments
Accepted Answer
Rik
on 11 Dec 2020
If you are using R2020b you can use the readlines function to read the content of a file to a string array. You can also get the readfile function from the FEX (or through the AddOn-manager, R2017a or later). The latter will read your file to a cell array of char arrays.
5 Comments
Rik
on 11 Dec 2020
No problem, you're welcome. If either answer solved your question, please consider accepting one of them. You can only accept one, but if you feel the other answer was helpful as well, you can give it an upvote.
More Answers (1)
Mathieu NOE
on 11 Dec 2020
hi
this is my suggestion
files = readcell('file_list.txt'); % read txt file to get mat filenames
% % this is just to generate some mat files
% for ci = 1:numel(files)
% data = rand(ci,ci);
% file_out = [char(files(ci)) '.mat'];
% save(file_out,'data');
% end
% main loop
for ci = 1:numel(files)
data = load([char(files(ci)) '.mat']);
size(data.data)
end
2 Comments
Stephen23
on 11 Dec 2020
Rather than using CHAR it is simpler to access the content of the cell array using the correct indexing:
char(files(ci)) % complex
files{ci} % correct indexing
SPRINTF is usually neater and more robust than string concatenation:
sprintf('%s.mat',files{ci})
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!