Hello, how can I do a datime vector. I have a vector whose name is T where I have date and time in this format : '03/19/2019 16:36:41', I want only the time so I write:
for(i=2:n)%I don't want the first row so I start from 2
a=T{i};
x(i,:)=a(11:end);
...
then I want to put every element of x (x is a matrix 1159x9) into a datetime vector so I do:
for (j=1:n-1)
H{j}=datetime(x(i,:),'Format','HH:mm:ss');
end
end
Matlab doesn't show any error but if I try to view H{34}, for example, it doesn't show me anything.

1 件のコメント

dpb
dpb 2019 年 3 月 25 日
>> t= '03/19/2019 16:36:41'; % your sample input form...
>> datetime(t(11:end))
ans =
datetime
25-Mar-2019 16:36:41
>>
shows that with datetime you simply cannot have just a time; there's always an implied date if not a specific one.
You can return arrays of the time components from a datetime array but you can't create the array in the beginning without a date being associated with the time. I, personally, tend to think this is an undesireable limitation as well, but it is what it is and I don't see TMW changing it.
Time-only vectors can only be some form of a duration; either calendar or absolute.
Your code not returning anything is an error don't see a cause for otomh, but you don't need a loop as datetime is vectorized for cell array of strings but you can't actually do what you're trying to do anyways...

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

 採用された回答

Star Strider
Star Strider 2019 年 3 月 25 日

3 投票

Just do something like this:
T = datetime('03/19/2019 16:36:41')
H = datetime(T,'Format','HH:mm:ss')
producing:
T =
datetime
19-Mar-2019 16:36:41
H =
datetime
16:36:41

3 件のコメント

dpb
dpb 2019 年 3 月 25 日
編集済み: dpb 2019 年 3 月 25 日
NB to OP: H still has the date associated with it, you simply can't get away from that, H is just formatted to only display the time portion.
>> T = datetime('03/19/2019 16:36:41');
H = datetime(T,'Format','HH:mm:ss');
>> T==H
ans =
logical
1
>>
As you see, they're both the same datetime value; just displayed as per the format property.
Whether this matters at all only depends on the use intended which we don't know...
Star Strider
Star Strider 2019 年 3 月 25 日
True for datetime arrays.
If you only want the time as a (1x8) character vector, do this:
H = datestr(T, 'hh:mm:ss')
producing:
H =
'16:03:41'
dpb
dpb 2019 年 3 月 25 日
Yeah, I still don't understand TMW's thinking on forcing the date to exist instead of just letting one have an array of times if that's all one wants for a given task.
With the venerable datenum since it's just a double(), one can do so easily by throwing away the integer portion but it has all the other issues associated with it so it's not all that attractive of a solution, either.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2019 年 3 月 25 日

編集済み:

dpb
2019 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by