How to save datetime values in an array?

조회 수: 26 (최근 30일)
yashvin
yashvin 2015년 6월 24일
댓글: Guillaume 2015년 6월 26일
Hi I am picking datetime value that is in this format
05-Feb-2015 18:02:47 05-Feb-2015 18:02:51 05-Feb-2015 18:02:55
I want to save it in an array so that later I can plot it. I am getting error to convert from datetime to double. What should i do?
  댓글 수: 6
yashvin
yashvin 2015년 6월 25일
Hi Guillaume,
Using your d1 for example,I had tried this this
time_match1(i,:)=datevec(d1));
and later
time_planned_way=datetime(time_match1)';
It is inside a for loop though. i will converting it in a string too! let me try
Guillaume
Guillaume 2015년 6월 25일
Clarification, the d1 example is nothing to do with me and the OP (Venky) appears to confuse the char type with the datetime time. He (she?) also does not seem to understand string to numeric conversion.

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

답변 (1개)

Guillaume
Guillaume 2015년 6월 24일
편집: Guillaume 2015년 6월 24일
If you're getting this error it's because you're trying to put a datetime object in a preinitialised array (possibly with zeros ?) of double. I presume you preinitialise the array, because the datetime objects are created / obtained in a loop.
You can't put datetime objects in a preinitialised array of double (you can only put doubles in there). The datetimes have to go in a preinitialised array of datetime. One simple way of initialising such an array:
darr = datetime(zeros(10,1), 0, 0); %a 10x1 array of datetime
Note that there will be a simpler way of doing this in the next version of matlab.
  댓글 수: 5
yashvin
yashvin 2015년 6월 26일
편집: yashvin 2015년 6월 26일
@ Guillaume
One more question: I am trying to save both a float or integer value together with a datetime value in an array. Anyway of making both these values cohabitate in an array?
d= 5.5
e = 05-Feb-2015 18:15:59
f= [d e]
Of course this is an error. But I am trying to map integers values to a datetime value. How i can do this. Should i change the datetime to a string?
Guillaume
Guillaume 2015년 6월 26일
A matrix can store data of only one type. So you can't put datetimes and float together, or float and integer together, or float and string together.
There are various kind of heterogeneous containers available in matlab, the simplest being cell arrays and tables
f1 = {d e}; %cell array
f2 = table(d, e, 'VariableNames', {'index', 'date'}); %table

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by