Change the format of all Datetime variables in a Table

46 ビュー (過去 30 日間)
Haris K.
Haris K. 2020 年 10 月 12 日
編集済み: Adam Danz 2023 年 5 月 8 日
Is there any way to change the format of all the datetime variables in a table?
Var1 = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17'});
Var2 = [20; 90]
Var3 = datetime({'2016-12-18 00:03:00';'2017-12-28 12:11:00'});
T = table(Var1, Var2, Var3);
S = vartype('datetime')
T{:,S}.Format = 'dd/MM/yyyy' %This does not work
What I would like to achieve is the result obtained from the following:
T.Var1.Format = 'dd/MM/yyyy'
T.Var3.Format = 'dd/MM/yyyy'
The thing though is that I am looping over different tables T, and at some iterations either (or both) Var1 and Var3 can be empty. Therefore, doing the latter method (i.e. setting the format of each variable individually) returns an error: Unable to perform assignment because dot indexing is not supported for variables of this type. Also, I have a lot more variables, so I would like to avoid if-isempty()-statements. Hence, I was wondering if there is a way to change the format of a few selected variables (-datetimes-) at once.

採用された回答

Walter Roberson
Walter Roberson 2020 年 10 月 13 日
編集済み: Adam Danz 2023 年 5 月 8 日
T = convertvars(T, @isdatetime, @(t) datetime(t, 'Format', 'dd/MM/yyyy'))
I tested to confirm that passing a datetime object as the parameter to datetime() can be used to construct new objects with different properties.
  4 件のコメント
Gabor
Gabor 2022 年 11 月 25 日
Undefined function 'convertvars' for input arguments of type 'datetime'.
Stephen23
Stephen23 2022 年 11 月 25 日
編集済み: Stephen23 2022 年 11 月 25 日
@Gabor: CONVERTVARS was introduced in R2018b. Are you using an earlier version?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by