Reading multiple files
조회 수: 10 (최근 30일)
이전 댓글 표시
Hello,
i´m writing a program that is supposed to read several files called regiao(1 through 10).txt. This is my program:
regiao=dir('regiao*.txt');
for k=1:10;
nome=regiao(k).name;
id=fopen(nome,"r");
while !feof(id)
for f=1:length(coluna);
[semana,infectados,mortes]=fscanf(id,"%s%s%s","C");
if !isempty(semana);
l(f).semana=semana;
l(f).infectados=infectados;
l(f).mortes=mortes;
f=f+1;
endif
endfor
endwhile
endfor
fclose(id);
endfunction
And this is what the .txt files look like:
Populacao:11000
Semana Infectados Mortes
8 29 13
35 290 148
My problem is that instead of retrieving all the information in the columns from all ten files my function only gets one value from each column. Can anyone help me?
댓글 수: 0
채택된 답변
Andrei Bobrov
2012년 5월 28일
try
regiao=dir('regiao*.txt');
for k=1:10;
nome=regiao(k).name;
id=fopen(nome,'r');
nms = textscan(id,'%s',4);
d = textscan(id,'%f %f %f',4);
fclose(id);
fd = regexp(nms{1}{1},'\w*','match');
dd = [fd(1), str2double(fd(2)); nms{1}(2:end), d']';
l(k) = struct(dd{:})
end
댓글 수: 0
추가 답변 (1개)
Walter Roberson
2012년 5월 27일
1) Your code is not MATLAB. MATLAB does not have "endif", or "endfor" or "endwhile"
2) What is "coluna" ?
3) index your "l" at (k,f) rather than at (f) alone, or else you end up overwriting "l" on every file.
4) The "f=f+1" is not useful there as you are in a "for f" loop.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!