How to save datetime values in an array?

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 件のコメント

venky
venky 2015 年 6 月 24 日
are u importing datetime values from excel sheet?
yashvin
yashvin 2015 年 6 月 24 日
I already imported it..while processing,..i am actually picking specific datetime values and saving it in an array.
I just tried using datevec and saving all the numbers in an array...any suggestion?
venky
venky 2015 年 6 月 24 日
you are trying to store character into numeric arry(double) without any conversion...it doesnt have any meaning.if you want to store in double type first convert string { str_numeric = uint16(str) } into str_nemeric and store this str_numeric in double type array. please refer the below code for ur persual.
>> d1
d1 =
24-Jun-2015 13:50:32
>> d2=12345
d2 =
12345
>> whos Name Size Bytes Class Attributes
d1 1x20 40 char
d2 1x1 8 double
Guillaume
Guillaume 2015 年 6 月 24 日
datetime is not characters! It's its own type.
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 日

1 投票

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 月 24 日
When i save the values using this kind of array, only i am getting my date...no time
Guillaume
Guillaume 2015 年 6 月 24 日
This kind of array is just a preinitialised array of datetimes. A datetime array always holds a time even if it does not display it. (For example if you type darr.Hour you will get the hours you stored in there)
You can change the display of the datetime with the Format property of the array. This will display the time:
darr.Format = 'default';
yashvin
yashvin 2015 年 6 月 25 日
Thanks! I am using this approach!
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2015 年 6 月 24 日

コメント済み:

2015 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by