Toomre Capital Markets LLC

Real-Time Capital Markets -- Analytics, Visualization, Event Processing, and Intelligence

SQL Server

Web Enabling MATLAB Analytics

Over the past several months, I’ve been working on several projects at Toomre Capital Markets LLC dealing with optimal ways of distributing results of calculations done in MATLAB. In previous posts, I’ve written about accessing and storing information from Excel and SQL databases. In this post, I want to look at distributing some of the results via web pages.

One approach that we’ve used is having a MATLAB routine run as a DLL called from an IIS web server. The webserver passes key parameters to the MATLAB routine. The routine then retrieves information from an SQL database, runs a set of analytical routines and stores the results back in SQL. The IIS server retrieves the results from the database and builds the webpage for the end users.

There is a lot that can be done using this methodology, but it can be complicated making sure that all the variables are properly mapped between Matlab, SQL, and the webpages. If you have an IIS server and an SQL Server up and running and have plenty of time, you can build a very powerful application this way.

Writing BLOBs from Matlab to SQL Server using ActiveX and Stored Procedures

Over the past few months, I’ve been working on a project that integrates sophisticated Matlab financial models with a Microsoft environment. Back in February, I wrote about writing from Matlab to Excel using ActiveX. Since then, I’ve been focusing on SQL Server and want to share those experiences.

The simplest way to access a Microsoft SQL Server from Matlab is to use the Database Toolbox. This toolbox allows for an ODBC or a JDBC connection to a database. Since our functions are running in a Microsoft environment, we went with an ODBC connection. The connection is very simple to open:

conn = database('ODBCdatasourcename','ODBCusername','ODBCpassword')

In our case, we would then ‘ping’ the connection to make sure everything has connected properly, and if not, return an error:

try
ping(conn) ;
catch ME
% insert error processing code here …
return;
end

We then execute our SQL and check the results

sql = ‘select a,b from c where d = 10’;
curs = exec(conn, sql) ;
curs = fetch( curs ) ;
NumRows = rows(curs);
if (NumRows < 1 )
% insert error processing code here about no rows returned
return ;
end

result = curs.Data;
for i=1:1:NumRows
if (~isnan(result{i,1}))
a{i} = result{i,1};
b{i} = result{i,2};
end
end
close(curs) ;
close(conn);

It has worked nicely for just about all of our needs. However, for speed and flexibility, we found it useful to use ActiveX for some of our database access.