I normally don’t delve too much into the programmatic details of my work, but I’ve seen enough interest in this topic that I figured I’d lend a helping hand to those fellow frustrated souls.
There are a few awkward, non-native MEX implementations of various database interfaces. However, Matlab has its own database toolbox built around ODBC/JDBC, and when developing distributable software, one always hopes to minimize third-party library usage. As a result, I’ve put a good deal of effort into integrating Matlab with both MySQL and SQLite. In fact, the data for every post on this site is stored in a 2GB MySQL database server running on my laptop.
As you can see, this requires only that you distribute the platform-independent JAR. No DLLs, no MEX compilation.
% Database Server host = 'localhost'; % Database Username/Passworduser = 'user'; password = 'password';% Database NamedbName = 'assets';% JDBC Parameters jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName); jdbcDriver = 'com.mysql.jdbc.Driver';% Set this to the path to your MySQL Connector/J JARjavaaddpath('mysql-connector-java-5.1.6-bin.jar')% Create the database connection object dbConn = database(dbName, user , password, jdbcDriver, jdbcString);% Check to make sure that we successfully connected if isconnection(dbConn) % Fetch the symbol, market cap, and last close for the 10 largest % market cap ETFs result = get(fetch(exec(dbConn, 'SELECT info.symbol,info.marketcap,series.close FROM info, series WHERE info.type=''ETF'' AND info.id = series.symbolid AND series.date = ''2008-04-11'' ORDER BY marketcap DESC LIMIT 10')), 'Data'); disp(result);% If the connection failed, print the error message else disp(sprintf('Connection failed: %s', dbConn.Message)); end % Close the connection so we don't run out of MySQL threads close(dbConn);
‘SPY’ [8.2300e+010] [133.3800]
‘EFA’ [4.5420e+010] [ 72.6400]
‘EEM’ [2.3850e+010] [139.3400]
‘GLD’ [1.9260e+010] [ 91.3000]
‘QQQQ’ [1.7040e+010] [ 44.2800]
‘IVV’ [1.6410e+010] [133.5200]
‘IWF’ [1.2860e+010] [ 55.2500]
‘DIA’ [1.0830e+010] [123.3400]
‘IWM’ [1.0470e+010] [ 68.7200]
‘VTI’ [9.7800e+009] [132.2900]