MatlabMysql
From FarmShare
Mysql queries in Matlab scripts
This document describes how to access mysql databases from your matlab scripts. Potentially any mysql database can be used, however this example was built around the mysql database service described here: https://itservices.stanford.edu/service/sql
Lets assume you have the following mysql database:
highlight: Unknown source file extension "mysql".
You need to specify a language like this: <source lang="html">...</source>
Supported languages for syntax highlighting:
abap4, abc, abnf, actionscript, ada, agda, algol, ampl, amtrix, applescript, arc, arm, as400cl, ascend, asp, aspect, assembler, ats, autohotkey, autoit, avenue, awk, bat, bbcode, bcpl, bibtex, biferno, bison, blitzbasic, bms, bnf, boo, c, ceylon, charmm, chill, clean, clearbasic, clipper, clojure, clp, cobol, coldfusion, crk, csharp, css, d, dart, diff, dylan, ebnf, eiffel, erlang, euphoria, express, fame, felix, fortran77, fortran90, frink, fsharp, fx, gambas, gdb, go, graphviz, haskell, haxe, hcl, html, httpd, icon, idl, idlang, inc_luatex, informix, ini, innosetup, interlis, io, jasmin, java, js, jsp, ldif, lhs, lilypond, limbo, lindenscript, lisp, logtalk, lotos, lotus, lua, luban, make, maple, matlab, maya, mercury, miranda, mod2, mod3, modelica, moon, ms, mssql, mxml, n3, nasal, nbc, nemerle, netrexx, nice, nsis, nxc, oberon, objc, ocaml, octave, oorexx, os, oz, paradox, pas, pdf, perl, php, pike, pl1, plperl, plpython, pltcl, pov, pro, progress, ps, ps1, psl, pure, pyrex, python, q, qmake, qu, r, rebol, rexx, rnc, rpg, rpl, ruby, s, sas, scala, scilab, sh, small, smalltalk, sml, snmp, snobol, spec, spn, sql, squirrel, sybase, tcl, tcsh, tex, ts, tsql, ttcn3, txt, upc, vala, vb, verilog, vhd, xml, xpp, yaiff, yang, znn
You can access this same data inside Matlab via (save this script as matmysql):
% Database Server host = 'mysql-user.stanford.edu'; % Database Username/Password user = 'gfarmsharetest'; password = 'putyourpasswordhere'; % Database Name dbName = 'g_farmshare_testing'; % 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 JAR javaaddpath('/usr/share/java/mysql-connector-java.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 foo,bar from matlabfoo')), 'Data'); disp(result); else % If the connection failed, print the error message disp(sprintf('Connection failed: %s', dbConn.Message)); end % Close the connection so we don't run out of MySQL threads close(dbConn);
submit the script to grid engine
See MATLAB for further information.
# #$ -cwd #$ -j y #$ -S /bin/bash #$ -N matlabmysql /afs/ir.stanford.edu/software/matlab-2011b/bin/matlab -nodesktop < matmysql
submit the job
$ qsub matlab_mysql.script
You should see following output:
Warning: No display specified. You will not be able to display graphics on the screen. Warning: No window system found. Java option 'MWT' ignored < M A T L A B (R) > Copyright 1984-2011 The MathWorks, Inc. R2011b (7.13.0.564) 64-bit (glnxa64) August 13, 2011 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> [1] [ 2] [2] [ 4] [3] [ 8] [4] [16] [5] [32]