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!