MATLAB: How to export dates and data to database

Database Toolboxdatabase toolbox datetime export

Hi I'm trying to export a table I've created in Matlab to an ODBC database. The table is set up with four columns (trade_date, ticker, curve_date, price). These columns are (datetime, char, datetime, double) datatypes, when i execute the command to write to my database (as i have before using only a table with all data being of type Double), i get the following:
Commands:
conn = database.ODBCConnection('DBName','','');
colnames = {'trade_dt','ticker','curve_dt','price'};
datainsert(conn, 'matlab.tblFwds', colnames, t);
summary(t) gives me:
summary(t)
Variables:
trade_dt: 110×1 datetime
Values:
min 19-Oct-2016
median 19-Oct-2016
max 19-Oct-2016
ticker: 110×21 char
curve_dt: 110×1 datetime
Values:
min 01-Nov-2016
median 16-May-2021
max 01-Dec-2025
price: 110×1 double
Values:
min 19.5
median 23.675
max 48.25
and the error i get when trying to insert into the database is:
Error using database.internal.utilities.DatabaseUtils.validateStruct (line 78)
Input structure, dataset or table incorrectly formatted. See help for details
Error in database.odbc.connection/insert (line 114)
data = database.internal.utilities.DatabaseUtils.validateStruct(data);
Error in database.odbc.connection/datainsert (line 40)
insert( connect,tableName,fieldNames,data );
Error in MorningstarHistoricalCurves (line 68)
datainsert(conn, 'matlab.tblGasFwds',colnames, t);
I would appreciate any help on how to insert this table/data into my database, any examples using dates would be greatly appreciated! Thanks in advance!
Matt

Best Answer

Hi Matt,
My name is Rikin Mehta. I am the developer working on the Database Toolbox with MathWorks.
Currently, Database Toolbox doesn't support datetime object with insert functionalities (i.e. INSERT/ FASTINSERT or DATAINSERT) for both JDBC and ODBC interface.
In order to insert columns containing datetime objects, you will need to manuallly type-cast the datetime object into date string using 'datestr(<your_datetime_object>,31)', before passing it to one of the insert functionalities.
Here is the documentation link with more details: http://www.mathworks.com/help/database/ug/datainsert.html
Apologies for this inconvenience. We will take a note of your inquiry and consider support for datetime objects in Database Toolbox for a future MATLAB release.
Rikin