如何将Java对象作为参数传递给MATLAB函数?

时间:2021-10-04 07:24:55

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

我编写了一个Matlab类来使用JDBC和java.sql中的东西来实现数据库。

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

我需要知道ResultSet中有多少结果,所以我编写了以下Matlab静态函数:

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'

???未定义的函数或方法'numRecords'表示'org.apache.derby.impl.jdbc.EmbedResultSet40'类型的输入参数

There are no other functions called numRecords.

没有其他称为numRecords的函数。

2 个解决方案

#1


2  

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!

显然,在Matlab类中,调用静态函数需要将封闭类预先添加到函数中...即使从同一个类中调用!

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

这是一个令人困惑的错误消息,因为Matlab不应该被打字,但它听起来像是......

#2


1  

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:

您应该能够像在任何其他变量/对象中一样处理MATLAB中的Java对象。您可以像这样创建一个Java对象:

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.

有关详细信息,请查看MATLAB文档。

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.

可能不言而喻,但是你应该避免给函数myFcn提供与你传入的Java对象的任何方法相同的名称(即重载)。关于哪些重载函数实际被调用,事情会变得混乱,正如另一个问题和我对它的回答所说明的那样。

#1


2  

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!

显然,在Matlab类中,调用静态函数需要将封闭类预先添加到函数中...即使从同一个类中调用!

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

这是一个令人困惑的错误消息,因为Matlab不应该被打字,但它听起来像是......

#2


1  

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:

您应该能够像在任何其他变量/对象中一样处理MATLAB中的Java对象。您可以像这样创建一个Java对象:

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.

有关详细信息,请查看MATLAB文档。

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.

可能不言而喻,但是你应该避免给函数myFcn提供与你传入的Java对象的任何方法相同的名称(即重载)。关于哪些重载函数实际被调用,事情会变得混乱,正如另一个问题和我对它的回答所说明的那样。