How to display datetime value or duration value in Edit Fields (Numeric or String)

조회 수: 26 (최근 30일)
R
R 2022년 6월 28일
댓글: dpb 2023년 2월 27일
I have two timestamps that I will like to display on my app created by App Designer. One is in datetime format and the other in duration. Now, I have tried using both the numeric edit field and the string edit field but I get the same error message.
Error using matlab.ui.control.EditField/set.Value (line 98)
'Value' must be a character vector or a string scalar.
Any ideas? Thanks!

답변 (2개)

dpb
dpb 2022년 6월 28일
Well, yes, do what the error says -- convert the datetime and/or duration to string representation.
>> dt=datetime(datestr(now)); % create a datetime variable
>> datestr(dt)
dt =
'28-Jun-2022 09:27:41'
>>
The format is/can be set by the 'Format' property of the datetime variable as desired.
>> du=timeofday(datetime(datestr(now))); % for duration
>> string(du)
du =
"09:30:52"
>> char(du)
du =
'09:32:08'
>> cellstr(du)
du =
1×1 cell array
{'09:32:19'}
>> datestr(du)
ans =
' 9:30 AM'
>> timeofday(datetime(datestr(now)))
>>
your choice -- NB: datestr on the duration doesn't preserve the leading zero with the default formatting.

Adi Purwandana
Adi Purwandana 2023년 2월 27일
I think the problem haven't been solved yet. Ive got the same problem. I need to get the difference/duration between two dates (up to hour, minute and seconds, not only day). It seems that app designer accomodates only "date picker" (mm/dd/yyy only, while hour-minute-seconds have been ignored). Anyone know this issue?
  댓글 수: 2
Steven Lord
Steven Lord 2023년 2월 27일
Subtract the two datetime arrays.
dt1 = datetime('now')
dt1 = datetime
27-Feb-2023 12:35:15
dt2 = datetime(2023, 2, 27, 16, 25, 36)
dt2 = datetime
27-Feb-2023 16:25:36
du = dt2-dt1
du = duration
03:50:20
Convert the resulting duration to a string.
str = string(du)
str = "03:50:20"
or split it into its component parts.
[h, m, s] = hms(du)
h = 3
m = 50
s = 20.7781
Use str, h, m, and/or s to create the data for your edit box.
dpb
dpb 2023년 2월 27일
@Steven Lord, I think the point @Adi Purwandana is stuck over is there isn't a user control to set time in app designer; how's the user to get the time iftself into the application to start with?
Looks like it's a "roll your own" issue to build...

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

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by