tags:

views:

504

answers:

2

This question popped into my twitter stream, so I'm not so personally interested in the answer yet, but when I do need the answer, I'd expect to find it here.

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

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

copying the rest of the question from Jim's Answer:

I needed 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.

+1  A: 

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.

gnovice
Thanks.. Now I've posted a tweet pointing the questioner's back to this question!
jbdavid
Glad to help! =)
gnovice
+1  A: 

I'm http://twitter.com/jimburnell, the one who originally asked this question on Twitter (thanks http://twitter.com/jbdavid).

Thank you a bunch for your answer, but I still have a problem I don't understand.

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

I needed 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.

...And as I was writing this, 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, cuz Matlab isn't supposed to be typed, but it makes it sound like it is...

Anyway, thanks both of you for your help!

@Jim, that often happens when you tell your problem to Darth. http://stackoverflow.com/questions/612537/why-is-it-that-properly-formulating-your-question-for-stackoverflow-often-yield/612555#612555
Scottie T