필터 지우기
필터 지우기

importing tsv files yyyy.mm.dd.tsv

조회 수: 1 (최근 30일)
Yassine
Yassine 2012년 7월 8일
Hi I'm beginner in MATLAB. I need to import tsv files that are named as follows:
2004.01.01.0000.tsv
2004.01.01.0300.tsv
2004.01.01.0600.tsv
2004.01.01.0900.tsv
2004.07.01.1200.tsv
2004.01.01.1500.tsv
2004.01.01.1800.tsv
2004.01.01.2100.tsv
2004.01.02.0000.tsv
2004.01.02.0300.tsv
2004.01.02.0600.tsv
2004.01.02.0900.tsv
2004.01.02.1200.tsv
2004.01.02.1500.tsv
2004.01.02.1800.tsv
2004.01.02.2100.tsv
...
...
2005.12.31.2100.tsv
so each day has 8 files associated with it.
I need to import them as a 3 dimensional matrix using for loops.
I have the idea that my code should look something like this but it is not easy as I think
for yy=2004:2005
y= int2str(yy);
for mm=1:12
m= int2str(mm);
for dd=1:31
d= int2str(dd);
for ii=0000:0300:2100
i=int2str(ii);
z(:,:,)=importdata(y.m.d.i);
end
end
end
end
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 8일
편집: Walter Roberson 2012년 7월 8일
yearvals = 2004:2005;
monvals = 1:12;
dayvals = 1:31;
hourvals = 0000:0300:2100;
for ynum = 1 : length(yearvals)
y = yearvals(ynum);
for mnum = 1 : length(monvals)
m = monvals(mnum);
for dnum = 1 : length(dvals)
d = dvals(dnum);
for hnum = 1 : length(hourvals)
h = hourvals(hnum);
filename = sprintf('%04d.%02d.%02d.%04d', y, m, d, h);
if exist(filename, 'file')
z(:, :, :, hnum, dnum, mnum, ynum) = importdata( filename );
end
end
end
end
end
... and better hope that the arrays are all the same size.
I ordered the indices as hour, day, month, year, because MATLAB is most efficient when the index that changes faster is to the left. You might find that for processing purposes, that a different arrangement is more efficient.
I should warn that with the above set-up, the non-existent days such as February 30th will have slots in the resulting array, but that zeros will be stored there. You can change that to another value such as NaN by putting an "else" on the "if exist". (That would fail, though, if it was initial files that were missing, such as if the data started from January 3rd because of trading holidays.)
  댓글 수: 2
Yassine
Yassine 2012년 7월 10일
Thanks
Walter Roberson
Walter Roberson 2012년 7월 10일
I think you want %02d not %01d

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by