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

41 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2015-6-18
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by