convert from datetimes to double

47 ビュー (過去 30 日間)
James Upton
James Upton 2019 年 7 月 16 日
コメント済み: Steven Lord 2019 年 7 月 16 日
I have created a variable called P_D and I want to put into it a date time asociated with each row (its financial data)
P_D(i,1)=data.Dates(i);
P_D(i,2)=data.Close(i);
P_D(i,3)=Premium;
When I run the code it is saying:
The following error occurred converting from datetime to double:
Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a
datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.
Error in test2 (line 38)
P_D(i,1)=data.Dates(i);
How do I convert to numeric? '09-Jul-2019 02:25:00'
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 16 日
What would you want the numeric value to be?
James Upton
James Upton 2019 年 7 月 16 日
anything which I can display on a chart as the date on the X axis and price on the Y axis. That is what I am trying to achieve.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 7 月 16 日
datenum() the datetime object.
This is not preferred. Consider using a table object or better yet a timetable
  2 件のコメント
James Upton
James Upton 2019 年 7 月 16 日
I want to display on a chart the date on the X axis and price on the Y axis. That is what I am trying to achieve.
Steven Lord
Steven Lord 2019 年 7 月 16 日
I second Walter's suggestion of storing your data in a timetable. Once you've done that, you could call plot on the times stored in the timetable as the first input and the numeric data as the second input.
t = datetime('today')+days(0:5).';
r = (0:5).'.^2;
tt = timetable(t, r);
plot(tt.t, tt.r)
Yes, I know in this example I could just as easily have plotted the data using t and r directly. But I'm assuming you may be performing some manipulation or analysis on the data before visualizing it, and many functions can accept a timetable.

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

カテゴリ

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