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

95 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jun 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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!