MATLAB Answers

Ajpaezm
0

Using Dates (if possible) as structure fields.

Ajpaezm
さんによって質問されました 2017 年 12 月 18 日
最新アクティビティ Peter Perkins
さんによって 回答されました 2017 年 12 月 19 日
I'd like to know if there's a way to use dates as struct fields. The data I want to store in the struct comes in a daily basis and I'd like to store it inside a struct. The thing is I cannot have field names such as "12/17/2017", nor "12_17_2017" because none of them begin with a letter and in the 1st case I have special characters such as "/" that are forbidden.
Is there a workaround you could suggest to save the data into a struct closely to the way I describe it?
Thanks for your time!

  3 件のコメント

Stephen Cobeldick
2017 年 12 月 18 日
Note that a much better date order is to use ISO 8601 format yyyy-mm-dd. This has the two major advantages that it is internationally unambiguous and also sorts correctly into chronological order. The format that you are using is neither of these.
Ajpaezm
2017 年 12 月 18 日
Thanks for pointing that out Stephen. I can re-arrange the formatting of the dates if required by using the ISO 8601 standard, but some API's I'm using to request data use dates as inputs and have a different format for their request. Sometimes the requirements are not standarized.
Walter Roberson
2017 年 12 月 19 日
Using datetime objects helps in those cases, as you can can simply set the Format property to the format you need for the purpose without having to do any explicit conversion of formats.

サインイン to comment.

2 件の回答

Walter Roberson
回答者: Walter Roberson
2017 年 12 月 18 日

See https://www.mathworks.com/help/matlab/ref/genvarname.html
Or prefix the date with a character and use strrep or regexprep to change the delimiters. Or use datestr to change the date format or change the Format property of a datetime object.

  0 件のコメント

サインイン to comment.


Peter Perkins
回答者: Peter Perkins
2017 年 12 月 19 日

It's quite possible that you could use a timetable, in which each row is labelled with a datetime. Hard to say without more info about what you are doing.

  0 件のコメント

サインイン to comment.



Translated by