Effacer les filtres
Effacer les filtres

How can I read all text files in a folder without making a struct?

41 vues (au cours des 30 derniers jours)
Robert
Robert le 18 Juin 2015
Commenté : Alex castilla le 16 Avr 2018
Using the function x = dir ('*.txt') gives a struct (1x127)
I want to read my 127 text files (matrices) in individually, not in a struct, how can I do it?
Alternatively how can I extract the 127 matrices from a struct?
Thank you in advance for any help

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Juin 2015
x = dir ('*.txt') does return a struct but it is a struct of information about the files, not a struct of the data. It basically tells you want the names of the files are (and sizes and last modified and things like that.) You still need to do the loading.
For example,
dinfo = dir('*.txt');
for K = 1 : length(dinfo)
thisfilename = dinfo(K).name; %just the name
thisdata = load(thisfilename); %load just this file
fprintf( 'File #%d, "%s", maximum value was: %g\n', K, thisfilename, max(thisdata(:)) ); %do something with the data
end
If you already know the names then you don't need to use dir() to tell them to you. For example,
for K = 1 : 42
thisfilename = sprintf('qwerty_%04d.txt', K);
thisdata = load(thisfilename); %load just this file
fprintf( 'File #%d, "%s", maximum value was: %g\n', K, thisfilename, max(thisdata(:)) ); %do something with the data
end
to load qwerty_0001.txt, qwerty_0002.txt ... qwerty_0042.txt
  5 commentaires
Alex castilla
Alex castilla le 16 Avr 2018
Modifié(e) : Alex castilla le 16 Avr 2018
In the "thisfilename " what is the "name"?
Alex castilla
Alex castilla le 16 Avr 2018
ok. I understood this is the file's name.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Sequence and Numeric Feature Data Workflows dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by