简体   繁体   English

如何将 Java object 作为参数传递给 MATLAB ZC1C425268E683845D1AB5074C11

[英]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 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:我需要知道 ResultSet 中有多少结果,所以我写了以下 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' Undefined function or method 'numRecords' for input arguments of type 'org.apache.derby.impl.jdbc.EmbedResultSet40'

There are no other functions called numRecords.没有其他称为 numRecords 的函数。

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! 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.) (好吧,它没有,但它至少调用了 function。)

It's a confusing error message, because Matlab isn't supposed to be typed, but it makes it sound like it is...这是一条令人困惑的错误消息,因为不应该键入 Matlab,但它听起来像是......

You should be able to treat a Java object in MATLAB just like any other variable/object.您应该能够像对待任何其他变量/对象一样对待 MATLAB 中的 Java object 。 You can create a Java object like this:您可以像这样创建 Java object:

myDate = java.util.Date;

and then pass that object to a function:然后将 object 传递给 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 (ie overloading ). 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 (ie 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.关于实际调用哪个重载 function 的事情可能会让人感到困惑,正如另一个问题我对它的回答所说明的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM