Problem with creating datetime and number array

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

Thank you for your suggestion:
dataTable = table(datetime(hrData.VarName1,'ConvertFrom','posixtime'),hrData.VarName2);
Thank you once again. :)

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

 채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 2일

0 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2020년 5월 2일

댓글:

2020년 5월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by