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

조회 수: 89 (최근 30일)
Robert
Robert 2015년 6월 18일
댓글: Alex castilla 2018년 4월 16일
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
Alex castilla
Alex castilla 2018년 4월 16일
편집: Alex castilla 2018년 4월 16일
In the "thisfilename " what is the "name"?
Alex castilla
Alex castilla 2018년 4월 16일
ok. I understood this is the file's name.

댓글을 달려면 로그인하십시오.

추가 답변 (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