Read a list of filenames in a text file

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

What exactly is your question? Do you want to generate the list of filenames?
to read and load so i can do things with it
so i guess generate the txt file list of filenames
This comment was flagged by sefki:
who pretends to want to help then airs rude tbh
Apparently you were hacked again. I restored your question from Google cache.
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

okay thank you i guess
Did I misunderstand? I though you had a txt file with a list of file names and wanted to read into a Matlab variable.
I think it would be more productive to explain what I misunderstood than to flag a comment asking for clarification.
sorry i was hacked very useful information thank you i was really struggling everything works fine now!
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)

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

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})
sure !
will keep that in a corner of my brain

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

on 11 Dec 2020

Commented:

on 16 Dec 2020

Community Treasure Hunt

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

Start Hunting!