jbdavid
jbdavid

Reputation: 622

How do I pass a Java object as a parameter to a MATLAB function?

I wrote a Matlab class to implement a database using JDBC and stuff from java.sql.

I need to know how many results were in a ResultSet, so I wrote the following Matlab static function:

methods (Static)

    function [numRecords] = numRecords(resultSet)
        numRecords = 0;
        if (~isempty(resultSet))
            row = resultSet.getRow();
            resultSet.beforeFirst();
            resultSet.last();
            numRecords = resultSet.getRow();
            resultSet.absolute(row);
        end
    end

end

But when I try to call it, I get the following error message:

??? Undefined function or method 'numRecords' for input arguments of type 'org.apache.derby.impl.jdbc.EmbedResultSet40'

There are no other functions called numRecords.

Upvotes: 1

Views: 1823

Answers (2)

Jim Burnell
Jim Burnell

Reputation:

As I was writing the original question, I realized my error.

Apparently, in a Matlab class, calling a static function requires the enclosing class to be prepended to the function...even when called from within the same class!

I replaced the line:

trials = zeros(numRecords(rs));

with

trials = zeros(CMAPSigSimResultsDB.numRecords(rs));

and it worked. (Well it didn't, but it called the function at least.)

It's a confusing error message, because Matlab isn't supposed to be typed, but it makes it sound like it is...

Upvotes: 2

gnovice
gnovice

Reputation: 125854

You should be able to treat a Java object in MATLAB just like any other variable/object. You can create a Java object like this:

myDate = java.util.Date;

and then pass that object to a function:

myFcn(myDate,...other input arguments...);

For more info, you can check out the MATLAB documentation.

EDIT:

It may go without saying, but you should avoid giving the function myFcn the same name as any of the methods for the Java object you are passing in (i.e. overloading). Things can get confusing with respect to which overloaded function actually gets called, as illustrated by this other question and my answer to it.

Upvotes: 1

Related Questions