Problem with creating datetime and number array

8 visualizaciones (últimos 30 días)
newton zeus
newton zeus el 2 de Mayo de 2020
Comentada: newton zeus el 2 de Mayo de 2020
Hello!
I'm trying to convert the first column to datetime format, then write the two columns to an array:
19036.055 68.5714
19036.906 70.4225
19037.758 70.4225
19038.633 68.5714
19039.547 65.6455
19040.469 65.0759
19041.367 66.8151
19042.219 70.4225
19043.117 66.8151
May I ask what is wrong with my code?
nDat = size(hrData.VarName1,1);
BPMdata = zeros(nDat,2);
for nCount = 1:nDat
BPMdata(nCount,1) = datetime(hrData.VarName1(1:end), 'ConvertFrom','posixtime');
BPMdata(nCount,2) = hrData.VarName2(1:end);
end
This is the error message: "The following error occurred converting from datetime to double: Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions."
Thank you!
  1 comentario
newton zeus
newton zeus el 2 de Mayo de 2020
Thank you for your suggestion:
dataTable = table(datetime(hrData.VarName1,'ConvertFrom','posixtime'),hrData.VarName2);
Thank you once again. :)

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Mayo de 2020
I'm trying to convert the first column to datetime format, then write the two columns to an array:
You cannot do that. Any one array (that is not a structured data type such as a cell array or table) must have the same data type for all of its elements. In order to put both a datetime and a numeric value into the same array, either the datatime must be converted to numeric or else the numeric value must be converted to datetime.
You could use a cell array with two columns, or you could use a table() object. You appear to already be using a table object, so it might make the most sense to use a table.
Or you could convert the datetime into a datenum.. those are numeric.

Más respuestas (0)

Categorías

Más información sobre Dates and Time 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