MatlabMysql

From FarmShare

(Difference between revisions)
Jump to: navigation, search
 
(4 intermediate revisions not shown)
Line 6: Line 6:
<br>  
<br>  
-
<pre>$ mysql -p --user=gfarmsharetest --host=mysql-user.stanford.edu g_farmshare_testing
+
<source lang="sybase">$ mysql -p --user=gfarmsharetest --host=mysql-user.stanford.edu g_farmshare_testing
Enter password:  
Enter password:  
Reading table information for completion of table and column names
Reading table information for completion of table and column names
Line 21: Line 21:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
mysql&gt; show tables;
+
mysql> show tables;
+-------------------------------+
+-------------------------------+
| Tables_in_g_farmshare_testing |
| Tables_in_g_farmshare_testing |
Line 29: Line 29:
1 row in set (0.00 sec)
1 row in set (0.00 sec)
-
mysql&gt; desc matlabfoo;
+
mysql> desc matlabfoo;
+-------+---------+------+-----+---------+-------+
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
| Field | Type    | Null | Key | Default | Extra |
Line 38: Line 38:
2 rows in set (0.00 sec)
2 rows in set (0.00 sec)
-
mysql&gt; select * from matlabfoo;
+
mysql> select * from matlabfoo;
+------+------+
+------+------+
| foo  | bar  |
| foo  | bar  |
Line 49: Line 49:
+------+------+
+------+------+
5 rows in set (0.00 sec)
5 rows in set (0.00 sec)
-
</pre>  
+
</source>
 +
 
You can access this same data inside Matlab via (save this script as matmysql):  
You can access this same data inside Matlab via (save this script as matmysql):  
<source lang="m">% Database Server
<source lang="m">% Database Server
Line 88: Line 89:
See [[MATLAB]] for further information.  
See [[MATLAB]] for further information.  
-
<source lang="sh">$ cat matlab_mysql.script
+
<source lang="sh">
-
#
+
#!/bin/bash
 +
 
#$ -cwd
#$ -cwd
-
#$ -j y
 
#$ -S /bin/bash
#$ -S /bin/bash
#$ -N matlabmysql
#$ -N matlabmysql
-
/afs/ir.stanford.edu/software/matlab-2011b/bin/matlab -nodesktop < matmysql  
+
module load matlab
 +
matlab -nodesktop < matmysql  
</source>  
</source>  
<br>  
<br>  

Latest revision as of 13:34, 14 September 2013

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:


$ mysql -p --user=gfarmsharetest --host=mysql-user.stanford.edu g_farmshare_testing
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with&nbsp;; or \g.
Your MySQL connection id is 186013282
Server version: 5.1.49-3~bpo50+1-log (Debian)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables;
+-------------------------------+
| Tables_in_g_farmshare_testing |
+-------------------------------+
| matlabfoo                     |
+-------------------------------+
1 row in set (0.00 sec)

mysql> desc matlabfoo;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  |     | NULL    |       |
| bar   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from matlabfoo;
+------+------+
| foo  | bar  |
+------+------+
|    1 |    2 |
|    2 |    4 |
|    3 |    8 |
|    4 |   16 |
|    5 |   32 |
+------+------+
5 rows in set (0.00 sec)

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:&nbsp;%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.

#!/bin/bash

#$ -cwd
#$ -S /bin/bash
#$ -N matlabmysql

module load matlab
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]
Personal tools
Toolbox
LANGUAGES