You are on page 1of 1

Matlab code to import data from MS Access

The following Matlab code can be used to connect to an MS Access Database and import a table into a Matlab data structure.

function out = CreateData(DBName, TableName) % Takes in an MS Access Database name and table name and returns a dataset s=['PROVIDER=MSDASQL;']; s=[s 'DRIVER={Microsoft Access Driver (*.mdb)};']; s=[s 'DBQ=' DBName ';']; % Timeout if connection to DB can't be made in 60s try cn=COM.OWC11 DataSourceControl 11; catch cn=COM.OWC10 DataSourceControl 10; end cn.ConnectionString=s; cn.Connection.CommandTimeout=60; cn.RecordsetType=1; % Limit rows returned to 20,000 sql=strcat('select top 200000 * from', TableName); r = cn.connection.invoke('execute', sql); if r.state && r.recordcount>0 x=invoke(r,'getrows'); x=x'; else x=[]; end invoke(r,'release'); out = x;

You might also like