Converting a timetable to a matrix

71 visualizaciones (últimos 30 días)
AA
AA el 6 de Abr. de 2018
Respondida: Peter Perkins el 11 de Abr. de 2018
Consider the following table
TimeStamp X1 X2 X3 X4
____________________ ______ ______ ______ ______
03-Jan-2005 01:08:00 1.9193 1.9193 1.9193 1.9193
03-Jan-2005 01:13:00 1.9193 1.9193 1.906 1.906
I tried converting it back to a matrix in order to get the following format:
732315 108 1.9193000 1.9193000 1.9193000 1.9193000
732315 113 1.9193000 1.9193000 1.9060000 1.9060000
Somehow I get an error with the following code:
TT=timetable2table(F);
numformat = datevec(TT.TimeStamp);
since_midnight=numformat(:,4)*60+numformat(:,5);
temp=regexp(TT.Timestamp,'\s+', 'split');
datecol=cellfun(@(C)C{1},temp,'uniform',0);
formatfreak='dd/mm/yyyy';
serialdates = datenum(char(datecol),formatfreak);
every=[sum(:,2),sum(:,3),sum(:,4),sum(:,5)];
matrix=cell2mat(every);
everyfull=[serialdates(:),since_midnight(:)];
x=[everyfull,matrix];
Error:
Error using regexp
The 'STRING' input must be either a char row vector, a cell array of char row vectors, or a string
  2 comentarios
dpb
dpb el 6 de Abr. de 2018
temp=regexp(ans,'\s+', 'split');
Looks like you pasted in a line from command window while testing; 'ans' is whatever it might happen to be in your code at the time you call regexp.
AA
AA el 6 de Abr. de 2018
I tried regexp(TT.TimeStamp,'\s+', 'split'); still getting the same error

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Abr. de 2018
Editada: Walter Roberson el 7 de Abr. de 2018
TT.TimeStamp is in datetime() or duration() format for a timetable() object.
daynumbers = floor(datenum(TT.TimeStamp));
HM = hour(TT.TimeStamp)*100 + minute(TT.TimeStamp);
output = [daynumbers(:), HM(:), TT{:,:}];
  2 comentarios
AA
AA el 7 de Abr. de 2018
*60
Walter Roberson
Walter Roberson el 7 de Abr. de 2018
No, *100. You have 01:13:00 and you want to see 113 from that. hour() of this is 1, and minute() of this is 13. If you multiplied the 1 by 60 and added 13 you would get 73, number of minutes into the day, which is potentially a valid thing to want to see, but your example output requested 113 which is 1*100+13

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 11 de Abr. de 2018
AA, I guess Walter has answered your question, but I can't think of a reason why you would want to convert your table containing datetimes into those datenum+otherThing values. Maybe there's other code somewhere that expects that, but otherwise, it seems like an unnecessary headache.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by