Tuesday, November 18, 2014

PostgresQL Database connection with MATLAB

Configuration Steps

Steps:

1. Download and copy JDBC driver for Postgres version from this link
http://jdbc.postgresql.org/download.html

2. Follow this link to edit JDBC driver path to Matlab java class path.
http://in.mathworks.com/help/database/ug/postgresql-jdbc-windows.html

3. Follow above link to complete the connection.


Matlab Data Retrieval from PostgresQL 

1. Connect to database

DataBase =  database('hpc','postgres','postgres','Vendor','PostgreSQL')

2. Conver num to string for comparison

startdate= num2str('2014-11-02');
endddate=num2str('2014-11-08');

3. SQL query and execute the query

sqlquery = ['select temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8 from racktemp where nodeid = 7 and systemtime BETWEEN (''',startdate,''') and (''',endddate,''')'];
curs = exec(DataBase,sqlquery);

4. Fetch the data and store in an array

curs = fetch(curs);
Data = curs.Data;
Data(15,2)

5. Close connection

close(DataBase);

6. Use integer variable inside Postgres query in Matlab

are = 10
sqlquery = ['select loadavg1min from punecpuload where sno =' num2str(are)]

7. Use string variable inside Postgres query in Matlab

nodeL = num2str('r4-c1-n1');
sqlquery = ['select count(*) FROM consotabletemp where node_id =(''',nodeL,''')'];

2 comments:

  1. Hi,
    I am trying to interface Soil moisture sensor with TelosB Motes.
    I need to read Analog value frm the sensor connected via ADC0 port of GPIO telosb pins.
    The components HplMSP430C is used to get only digital values.
    I need to combine Read interface with HplMSP430 to get the analog values.
    Is it Possible? Or is der any other components to read the analog values.

    Kindly suggest.

    ReplyDelete
  2. You could also use PgMex(http://pgmex.alliedtesting.com/) - high-performance PostgreSQL client library for Matlab that enables a Matlab-based application to communicate with PostgreSQL database in the Matlab native way by passing data in a form of matrices, multi-dimensional arrays and structures. The library is written in pure C which gives a significant performance boost for both small and data-heavy database requests. Both Windows and Linux platforms are supported.

    ReplyDelete