Skip to content

Instantly share code, notes, and snippets.

@biogeo
Created December 3, 2013 02:50
Show Gist options
  • Save biogeo/7763166 to your computer and use it in GitHub Desktop.
Save biogeo/7763166 to your computer and use it in GitHub Desktop.
How to set up Matlab to connect to MySQL using the Database Toolbox on Ubuntu
This information is partially based on advice from here: http://www.ahowto.net/matlab/connect-and-access-mysql-database
It's assumed that Matlab, the Database Toolbox, and MySQL are already installed.
1. The JDBC driver needs to be installed to allow Matlab to connect to MySQL using Java. I installed it using the package manager:
$ sudo apt-get install libmysql-java
2. On my system, that installed the JDBC driver at /usr/share/java/mysql-connector-java.jar . Matlab needs to be informed about this. In Matlab:
>> javaaddpath('/usr/share/java/mysql-connector-java.jar')
I think this needs to be called in every Matlab session, so it might be good to go ahead and add it to startup.m .
3. The Database Toolbox doesn't seem to allow you to create databases? So create one externally:
$ mysql -u root
mysql> CREATE DATABASE testdb;
replacing "root" and "testdb" with whatever user and database names are appropriate, of course.
4. Check that the connection works in Matlab:
>> dbConn = database('testdb', 'root', '', 'com.mysql.jdbc.Driver', 'jdbc:mysql://localhost/')
with 'testdb' and 'root' replaced with whatever's appropriate. Make sure there are no errors and that everything looks good.
That's it. Add data to tables in the database using the database method datainsert, execute queries using the method exec, and get the results of queries into Matlab using the method fetch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment