I'm using Matlab R2015b. I have "date" variable ( 178x1 cell ) inside a struct named "data". It consists of one column with multiple rows that consists of dates in dd/mm/yyyy format. Now I'm trying to convert them into numbers using datenum command,
dates = datenum(data.date, 'dd/mm/yyyy');
but it returns me with error.. What am I doing wrong? I've attached a screenshot of what the data looks like. Thanks in advance.

1 件のコメント

Adhi Ariawan
Adhi Ariawan 2017 年 11 月 29 日
Additional note: I noticed I have non-date data on the first row.. How do I treat the first row as NaN data?

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

 採用された回答

Star Strider
Star Strider 2017 年 11 月 29 日

0 投票

Note that the first row is the string ‘date’. The datenum function will fail on that, so start with the second row instead.

4 件のコメント

Adhi Ariawan
Adhi Ariawan 2017 年 11 月 29 日
How can I do that? haha (Sorry I'm newbie)
Star Strider
Star Strider 2017 年 11 月 29 日
My general solution is for you to address the rows of your array as:
dates = datenum(data.date(2:end), 'dd/mm/yyyy');
This is a guess, so make any necessary changes.
Adhi Ariawan
Adhi Ariawan 2017 年 11 月 29 日
Sorry to bother again, your solution was great, but after using (2:end) I'm facing another problem:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
I got that error when trying to process the new 'date' with the other data inside of struct. So I was wondering if we can keep the first row as NaN or something. Thanks again.
Star Strider
Star Strider 2017 年 11 月 29 日
My pleasure.
You have to use 2:end with every column.
No worries!

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

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2017 年 11 月 29 日

0 投票

If you have R2015b, you will likely be happioer using datetimes rather than datenums:
dates = datetime(data.date(2:end),'Format','dd/mm/yyyy')
You may also want to use a table, rather than a scalar struct, but hard to say without seeing more.

カテゴリ

ヘルプ センター および File Exchange で Dates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by