how to get matlab to talk to mysql
February 10th, 2006 by Lawrence David
i’ve been working on a gene identification / network modeling experiment for a couple of weeks now. i have tons of processed microarray data that now needs to be examined as well as further annotated by hand. since all the data has already been processed, a spreadsheeting package is unncessary; what i really need is a database to store all this information that doesn’t fit neatly into fixed rows and columns.
unfortunately, the easiest piece (read, nicest GUI) of database software around, Microsoft Access, doesn’t live on OS X. filemaker pro is also out, since i’d like to write perl scripts to interface with the database. next up was MySQL. this looked like a good option, especially since all the processed data currently lives in matlab; matlab advertises a database toolbox that eases transfer of data from matlab to MySQL.
here’s how i figured out how to get this working (on mac OS X):
1) download and install mysql.
2) download the jdbc driver here.
3) open the classpath.txt file in [matlab path]/toolbox/local/ and add the following line to it:
[path to unzipped jdbc driver package]/mysql-connector-java-3.1.12-bin.jar
4) create a database in mysql (can look up here how to do that)
5) to connect to the database you’ve created (call it “foo”), type into matlab:
>> conn = database(‘foo’,’[your user name]‘,”,’com.mysql.jdbc.Driver’,'jdbc:mysql://localhost:3306/foo’)
you can go ahead and try to use matlab’s confds command; i spent over an hour trying to get it to work with no luck. i have no idea why the command-line option works but the GUI one doesn’t.
6) start reading the sql and mathworks documentation, like i’m doing now, and learn how to be useful.
a note: this piece of documentation proved somewhat useful throughout this process; it’s actually more informative than the most current page on the mathworks support site.
Thank you very much for your detailed description how to connect Matlab to mysql with MAC OS 10.4. I have spent many useless hours to get it done. The problem was, there is a debuged JDBC driver (mysql-connector-java-3.1.12-bin-g.jar) that does not work. Maybe worth to know.
Best
Christian
mabe you can try this http://itswww.epfl.ch/~maret/software.php
I agree, this description is incredibly helpful!
Thanks,
-Stephen
Anyone got a compiled version of the above working?
Seems like it is troublesome to set the classpath…
In particular, the error:
Undefined variable: “com.mathworks.toolbox.database.databaseConnect”
Ian.
i faced an eror
Undefined variable: “com.mathworks.toolbox.database.databaseConnectâ€
Hi ,
I went around here when searching how to work in matlab linked with mysql. Thanks alot for your help ( and I believe lots of people have been grateful to your amazing summarized information ragarding this)!
I just wonder if I can create new tables via matlab to the database, or in other words, can we work in matlab just like we do in mysql itself? (and the only difference is just different language ?)
cheers,
Yue
Your my man THANKS++++++…
Thank a lot for the clear and useful example.
Honza M.
Hello,
I have done (to the best of my ability) exactly what was outlined above. However conn.Message continues to say:
“JDBC Driver Error: com.mysql.jdbc.Driver. Driver Not Found/Loaded.”
Does anyone have any ideas? Thanks in advance for your time.
-David
I’ve been trying for >1 hour. With your description it took me 5 min.
Thank a lot.
JK
hey i am getting a getting an error.
Communications link failure
Last packet sent to the server was 0 ms ago.
plz help
[...] Connection between Matlab and MySQL Posted 15 November 2009 Filed under: Uncategorized | Tags: code, matlab, mysql | here’s: http://desk.stinkpot.org:8080/tricks/index.php/2006/02/how-to-get-matlab-to-talk-to-mysql/ [...]
Thanks, I have exactly the same situation; no luck with GUI but your post encouraged me to try connecting in the command window which worked perfectly. Any idea yet why the GUI is broken?
Here’s a solution that doesn’t require the database toolbox: http://www.mathworks.com/matlabcentral/fileexchange/28237.
I found this website: http://www.cims.nyu.edu/~almgren/mysql/ which also does not need the database toolbox.
I have written a step by step installation instructions for Linux 64bit:
http://www.ramonfincken.com/permalink/topic238.html
Hey ,,
Thanks for the post…
Quite helpful..
I am trying to write an entire variable into the databse with 1440 values in each variable.. Could you suggest the way forward in doing this ?
sRiram
[...] http://desk.stinkpot.org:8080/tricks/index.php/2006/02/how-to-get-matlab-to-talk-to-mysql/ [...]
I am working on ubuntu OS. I am confronting an error
“Undefined function or variable ‘openmysqlconnection’”. openmysql connection is the function which includes all the data to connect to mysql db.
try this
url = ‘jdbc:mysql://localhost:3306/’;
conn = database(‘matlab_db’, ‘root’, ”, ‘com.mysql.jdbc.Driver’, url);
curs = exec(conn,’SELECT * FROM barang’);
curs = fetch(curs)
curs.data
tableName = ‘barang’;
fields = {‘kode_barang’, ‘nama_barang’, ‘deskripsi’, ‘harga’,'kategori’, ‘gambar’, ‘status’};
%allData = {‘BRG004′, ‘KOMPUTER 4′, ‘Harga murah…..harga ok’, ’1250000′, ‘Komputer’, ‘kom4.jpg’, ‘Tersedia’};
var1=get(handles.guivar1,’String’);
var2=get(handles.guivar2,’String’);
var3=get(handles.guivar3,’String’);
var4=get(handles.guivar4,’String’);
var5=get(handles.guivar5,’String’);
var6=get(handles.guivar6,’String’);
var7=get(handles.guivar7,’String’);
allData = {var1, var2, var3,var4,var5,var6, var7};
tic
fastinsert(conn,tableName,fields,allData)
toc
% clear the table
% deleteQuery = ['delete from ',tableName,';'];
% exec(db,deleteQuery);
or more detail:
http://matlab-adiarray.blogspot.co.id/2016/02/matlab-lihat-dan-simpan-data-di-mysql.html
Man, I have spent two days on trying to connect matlab to oracle database server. Mathworks guides are not helpful at all. Your post helped me in 5 minutes! Thank you so much!!!