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

4 ビュー (過去 30 日間)
R
R 2022 年 6 月 28 日
回答済み: dpb 2022 年 6 月 28 日
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!

回答 (1 件)

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.

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by