Using datenum with a structure field

3 ビュー (過去 30 日間)
Dave
Dave 2017 年 12 月 29 日
回答済み: Peter Perkins 2018 年 1 月 5 日
I'm working with a structure called data and there is a data.DATE field with values in format of 'yyyy-mm-dd HH:MM'.
When I try to replace the values in the structure field with
data.DATE = datenum(data.DATE, 'yyyy-mm-dd HH:MM');
I get an error of
Error using datenum
Too many input arguments.
This works fine for a cell array but not for the structure field. I guess I could split the data.DATE using regexp but was hoping to get this done in one line.
Any help / suggestions would be appreciated.

採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 30 日
data.DATE = datenum({data.DATE}, 'yyyy-mm-dd HH:MM')
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 12 月 30 日
temp = num2cell( datenum({data.DATE}, 'yyyy-mm-dd HH:MM') );
[data.DATE] = temp{:};
Dave
Dave 2017 年 12 月 30 日
That did it. Thanks

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

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2018 年 1 月 5 日
As Walter pointed out, there were two things going on, both having to do with the fact that
s.Field
for a non-scalar struct is a complicated thing. Not enough to go on in your post to know for sure, but you might find that replacing your struct array with a table (or even a timetable, in 16b+) makes your like easier. Unless you have hierarchical data (and even then, some of the time), tables are usually more convenient for messing with data.
And as always, consider using datetime instead of datenum.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by