How to open DBase files (.DBF) in Matlab?

后端 未结 3 1189
忘掉有多难
忘掉有多难 2021-01-12 18:52

I\'ve googled and searched through Matlab Central, but cannot find any way to open DBF files directly in Matlab. There are some references to DBFREAD function in TMW File Ex

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 19:30

    The way I see it, you have two options:

    Method 1: use ODBC to read the dBASE files:

    This requires the database toolbox

    cd 'path/to/dbf/files/'
    conn = database('dBASE Files', '', '');
    cur = exec(conn, 'select * from table');
    res = fetch(cur);
    res.Data
    close(conn)
    

    'dBASE Files' is an ODBC Data Source Name (DSN) (I believe its installed by default with MS Office). It uses the current directory to look for .dbf files.

    Or maybe you can use a DSN-less connection string with something like:

    driver = 'sun.jdbc.odbc.JdbcOdbcDriver';
    url = 'jdbc:odbc:DRIVER={Microsoft dBase Driver (*.dbf)};DBQ=x:\path;DefaultDir=x:\path';
    conn = database('DB', '', '', driver, url);
    ...
    

    if this gives you trouble, try using the FoxPro ODBC Driver instead..

    For Linux/Unix, the same thing could be done. A quick search revealed these:

    • How do set up an ODBC DSN on Mac or Linux/Unix
    • how to create and use dBase-format files with OpenOffice

    Method 2: read/write .dbf files directly with a library

    There's a Java library available for free from SVConsulting, that allows you to read/write DBF files: JDBF. (UPDATE: link seems to be dead, use Wayback Machine to access it)

    You can use Java classes directly from MATLAB. Refer to the documentation to see how.

提交回复
热议问题