Borrar filtros
Borrar filtros

importing tsv files yyyy.mm.dd.tsv

1 visualización (últimos 30 días)
Yassine
Yassine el 8 de Jul. de 2012
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jul. de 2012
Editada: Walter Roberson el 8 de Jul. de 2012
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 comentarios
Yassine
Yassine el 10 de Jul. de 2012
Thanks
Walter Roberson
Walter Roberson el 10 de Jul. de 2012
I think you want %02d not %01d

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by