Read a list of filenames in a text file
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';
Accepted Answer
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})
Mathieu NOE
on 11 Dec 2020
sure !
will keep that in a corner of my brain
Categories
Find more on Data Import and Export 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!