How can I convert a datetime vector to cell??

92 ビュー (過去 30 日間)
Adriano
Adriano 2016 年 11 月 17 日
コメント済み: Peter Perkins 2016 年 11 月 18 日
hi all,
I have a vector of 4000 dates like:
25/07/1996;
26/07/1996;
29/07/1996;
30/07/1996;
31/07/1996;
01/08/1996;
02/08/1996;
05/08/1996;
06/08/1996;
07/08/1996;... and so on
Now, they are in datetime format. My goal is to insert the dates like rows names into a table. For example:
Age Height Weight BloodPressure
___ ______ ______ _____________
25/07/1996 38 71 176 124 93
26/07/1996 43 69 163 109 77
I know that the rows names of a table must be cell inputs. Thus, how can I do it? Thanks a lot!

採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 17 日
編集済み: Walter Roberson 2016 年 11 月 17 日
Example:
YourTable = array2table((1:6).')
temp = cellstr( datestr(732430:732435) );
YourTable.Properties.RowNames = temp;
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 17 日
YourTable = array2table((1:6).')
temp = cellstr( datestr(732430:732435, 'dd/mm/yyyy') );
YourTable.Properties.RowNames = temp;
I think you did not notice the datestr()
Adriano
Adriano 2016 年 11 月 17 日
Great!!! Many thanks.

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

その他の回答 (2 件)

Guillaume
Guillaume 2016 年 11 月 17 日
Rather than using obsolete datestr, simply convert the datetime with char (which will use the Format of the datetime:
array2table(somematrix, 'RowNames', cellstr(char(datetimevector)))
Note that R2016b introduces the timetable that makes this even simpler:
array2timetable(somematrix, 'RowTimes', datetimevector)
  1 件のコメント
Peter Perkins
Peter Perkins 2016 年 11 月 18 日
You can even go directly to a cellstr, without char in the middle:
array2table(somematrix, 'RowNames', cellstr(datetimevector))

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


Peter Perkins
Peter Perkins 2016 年 11 月 18 日
Adriano, row names are really convenient when you want to use them for subscripting. But in this case, you need to consider that you are taking a datetime vector and turning it into text. So you lose all the calendar arithmetic that datetime provides, and you will no longer be able to do things like
T(T.Date > '1-Jan-2016',:)
that you would be able to do if you put the datetime vector into your table as a variable rather than as the row names. It depends on what you want to do.
As Guillaume pointed out, the new-for-R2016b timetable type kind of gives you the best of both worlds, a timetable's "row names" are actually row times, stored as either datetimes or durations.

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by