Home > About Myself, Programming, Technology > Connect MS-Access database from MATLAB

Connect MS-Access database from MATLAB


Already I’ve shared the way and source code how to connect MySQL data base from MATLAB. In this post I want to share how to connect to MS-Access. I initially thought of using the MySQL as database but due to some issues of reinstalling the OS and being MySQL not getting installed properly, I went for MS-Access. I hope this post will be very helpful to you if you have come here searching for connecting MS-Access from MATLAB.

function db=insertdb(name, imagePath, ICC)
dbpath = 'C:\Users\MyWindows7\Documents\abc.mdb';
conurl = ['jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=' dbpath];
con = database('','','','sun.jdbc.odbc.JdbcOdbcDriver', conurl)

%insertQuery=['insert into Image values(' name ',' imagePath ',' ICC ');' ]
%e = exec(con,insertQuery);
colnames={'imgName', 'imagePath', 'iCC'};
values={name, imagePath, ICC};
insert(con, 'Image',colnames, values ) %This statement inserts the values which are contained by values %array variables
%e = fetch(e);
%data = e.Data

In the above program % represents comments you can ignore them if you want. I included them to say that there are several other ways to insert a row into the MS-Access database. In the above code  if you specify the path of database(ie.,        .mdb file) then its enough there is no need for you to specify DSN name and other things. similarly you can also work with other SQL statements. I hope this helps. If you have any more queries or clarifications you can feel free to comment here.

Advertisements
  1. Sophia
    May 6, 2011 at 8:04 am

    Hi, i’ve tried to run this (1) in Matlab (R2007b) and it gives me back this (2). It will be much appreciated if you could give me an answer that will solve my problem. Thank you.

    (1)
    ———————————————————————————
    logintimeout(30);

    dbpath = ‘C:\Documents and Settings\UnFelDeRoshuAprins\Desktop\aplicatie licenta reloaded\SampleDB.mdb’;
    conurl = [‘jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=’ dbpath];
    con = database(”,”,”,’sun.jdbc.odbc.JdbcOdbcDriver’, conurl)

    curs=exec(con, ‘select all CNP from Profesori’);
    setdbprefs(‘DataReturnFormat’,’numeric’)
    curs=fetch(curs,3);
    curs.Data
    ————————————————————————————

    (2)
    ————————————————————————————
    >> connect

    con =

    Instance: ”
    UserName: ”
    Driver: []
    URL: []
    Constructor: [1×1 com.mathworks.toolbox.database.databaseConnect]
    Message: [1×163 char]
    Handle: 0
    TimeOut: []
    AutoCommit: ‘off’
    Type: ‘Database Object’

    ??? Undefined function or method ‘fetch’ for input arguments of type ‘struct’.

    Error in ==> connect at 9
    curs=fetch(curs,3);
    ———————————————————————————–

    • May 8, 2011 at 5:48 pm

      Undefined fetch indicates that the data is not in the curs that means the “select” statement has not been executed properly…There could be database connectivity problem or select statement syntax problem in general.. In your case check whether the database exists or not.. or table name correct or not etc., For this database connectivity you can also use the database toolbar in the matlab This is an easy way(GUI based) to check above aspects.. Good luck..

  2. Saima
    April 21, 2012 at 1:54 pm

    Hey thanx for ur guidance but can u plz help me….
    I wanted a code about how i can access a database in access from matlab. My access data base contains videos and audio files . I want to play those files in my matlab GUI with a data analysis of those files such as plots displaying the FFT etc.
    With kind regards
    Saima

  3. kaleem
    April 24, 2012 at 9:32 am

    Hello i have done the above and also used
    a=isconnection(conn)
    ping(conn);

    to check connection but the following result came
    con =
    Instance: ”
    UserName: ”
    Driver: ‘sun.jdbc.odbc.JdbcOdbcDriver’
    URL: ‘jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=D:\kaleem\images\trycbir\CBIR Final\db3.mdb’
    Constructor: [1×1 com.mathworks.toolbox.database.databaseConnect]
    Message: []
    Handle: [1×1 sun.jdbc.odbc.JdbcOdbcConnection]
    TimeOut: 0
    AutoCommit: ‘on’
    Type: ‘Database Object’

    a =

    0

    ??? Error using ==> database.ping at 153
    Invalid connection.

    Error in ==> Untitled at 8
    ping(conn);

  4. HARI
    May 13, 2012 at 12:31 pm

    AM I NEED SQL SERVER SOFTWARE FOR CONNECTING MS ACCESS TO MATLAB?

  5. Eleftheria
    June 30, 2012 at 7:40 am

    ??? Error using ==> database.insert at 42
    Invalid connection.

    Error in ==> insertdb2 at 10
    insert(con, ‘Image’, colnames, values);

    Can anybody help please???

  6. dinesh
    November 26, 2012 at 5:57 am

    sir I need the database connectivity for exporting matlat.mat file data to the ms access

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: