Read a list of filenames in a text file

10 views (last 30 days)
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
Rik
Rik on 16 Dec 2020
Apparently you were hacked again. I restored your question from Google cache.
sanguini
sanguini on 16 Dec 2020
yes i think i was! I changed my password immediately!

Sign in to comment.

Accepted Answer

Rik
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
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.

Sign in to comment.

More Answers (1)

Mathieu NOE
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
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
Mathieu NOE on 11 Dec 2020
sure !
will keep that in a corner of my brain

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!