Hello everyone,
i am importing Data from a Database and want to save the timestamps and the values in a timetable. However the Time gets imported as System.DateTime, which I cannot use in a timetable, at least I get an error, when I try.
The System.DateTime 'netTime' looks like this:
K>> netTime(1)
ans =
DateTime with properties:
Date: [1×1 System.DateTime]
Day: 29
DayOfWeek: Thursday
DayOfYear: 119
Hour: 6
Kind: Unspecified
Millisecond: 300
Minute: 0
Month: 4
Now: [1×1 System.DateTime]
UtcNow: [1×1 System.DateTime]
Second: 0
Ticks: 637552728003000000
TimeOfDay: [1×1 System.TimeSpan]
Today: [1×1 System.DateTime]
Year: 2021
MinValue: [1×1 System.DateTime]
MaxValue: [1×1 System.DateTime]
What i Want is to only the Time hh:mm:ss:ms from this "cell" to use in a TT.
Can anyone tell me what I need to do?
Thanks in advance

 채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 6월 18일

1 개 추천

This converts the System.DateTime variable returned by netTime to a datetime variable, which can then be used with timetable:
t = netTime(1); % t is System.DateTime variable
% convert to datetime variable
t2 = datetime(t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second, t.Millisecond)

댓글 수: 7

Unfortunately, when I try:
netTime.Year
I get
No appropriate method, property, or field
'Year' for class 'System.DateTime[]'.
as an Error Message.
netTime(1).Year
Gives me the year of the first Data point.
I should also mention that netTime has a size of 325670.
is there anything else i can try?
Scott MacKenzie
Scott MacKenzie 2021년 6월 18일
There's a big difference between netTime and netTime(1). netTime(1) is just one System.DateTime element. And, yes, netTime(1).Year gives the year value for the SystemDateTime elment in position 1 of netTime. BTW, netTime(1).Year (your code) is the same as t.Year (my code). Did you try the code in my answer? I'm still thinking it should work
I am sorry, I misread your answer and you are correct. Your suggested code works.
However, the datetime won't show the Milliseconds. I tried to add
datetime(t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second, t.Millisecond, 'InputFormat', 'yyyy-mm-dd HH:MM:SS.SSS');
but i will still not consider the Millisenconds. In this case they are needed for calculations.
Any ideas how to involve them?
Walter Roberson
Walter Roberson 2021년 6월 21일
InputFormat is only used when parsing a character vector or string object. You should be using Format to control what is displayed.
Thank you Scott and Walter, the Code is now running without problems.
Since it is now working, I have a question concerning the performance. Although Scott's code is working, it is quite slow, are there any possibilities to increase speed?
Scott MacKenzie
Scott MacKenzie 2021년 6월 22일
You're welcome.
Obviously, issues with execution time relate to retrieving data from the database. I really don't have any insights to offer on this. Sorry.
Steven Lord
Steven Lord 2021년 6월 22일
Profile the code (possibly on a small sample of your data) to identify the bottlenecks. Depending on what the code in the bottlenecks is doing it may be possible to speed them up, but it could be difficult if not impossible to offer specific guidance without seeing the actual code you're executing.

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by