从 Matlab 调用 Java 时如何避免歧义?
我刚刚发现,当从 Matlab 调用 Java 时
object.method(arg1,...,argn)
相当于
method(object, arg1,...,argn)
这里的问题是我还有一个 method.m
可以进行从 Java 到 Matlab 的一些转换(例如,转换 String[] 到字符串单元格)。 我的
method.m
看起来像
function result = method(object, arg1,...argn)
intermediate = object.method(arg1,...argn);
result = translate(intermediate);
发生的事情是当我调用 method(object, arg1,...,argn)
时,它会直接进行 Java 调用,而不是我的使用我的 method.m
修复很简单,只需不要对我的 Java 方法和 .m
文件使用相同的方法名称即可。 但是还有别的办法吗? 我如何知道同名的方法将被调用? 有没有办法确保我调用 method.m
而不是 Java 方法? 确保调用 Java 方法很容易,只需使用 object.method语法。
附带说明一下,同样愚蠢的是 .m
编辑器链接到 method(object, arg1,...,argn) 上的
调用,而调试时则调用 Java 方法。method.m
)
I just discovered that when calling Java from Matlab
object.method(arg1,...,argn)
is equivalent to
method(object, arg1,...,argn)
The problem here is I also have a method.m
that does some translation from Java to Matlab (eg. convert String[]
to cell of strings). My method.m
looks like
function result = method(object, arg1,...argn)
intermediate = object.method(arg1,...argn);
result = translate(intermediate);
What is happening is when I call method(object, arg1,...,argn)
, it does the direct Java call, instead of my using my method.m
The fix is easy, just don't use the same method name for both my Java methods and my .m
files. But is there another way? How do I know which method will be called given the same name? Is there a way to ensure I call method.m
instead of the Java method? Its easy to ensure a call to the Java method, just use the object.method
syntax.
As a side note, what is also silly is the .m
Editor links to the method.m
on the method(object, arg1,...,argn)
call, while when it debugs it calls the Java method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会遇到 MATLAB 如何进行调度的一些问题...
我如何知道同名的方法将被调用?
MATLAB 文档的此部分讨论在存在多个函数的情况下如何选择函数具有相同名称的函数。 文档中:“函数优先顺序根据函数类型及其在 MATLAB 路径上的位置确定一个函数相对于另一个函数的优先级。” 这个顺序(从最高到最低)如下:
“method.m”函数的位置可能会决定它是否被调用或 Java 方法是否被调用使用“方法(对象,...)”语法时调用。
有没有办法确保我调用method.m而不是Java方法?
现在,我猜测您的“method.m”位于当前目录或路径上的其他位置(这两个最低优先级位置)。 如果您将“method.m”设为子函数 在调用它的较大代码中,或者是否可以将其放入 私有目录,每个需要调用它的函数都可以调用它,那么当您使用“方法(”时,它可能会被调用而不是 Java方法对象,...)”语法。
希望这可以帮助!
You may be running into some problems with how MATLAB does dispatching...
How do I know which method will be called given the same name?
This section of the MATLAB documentation discusses how a function is chosen in cases when there are multiple functions with the same name. From the documentation: "The function precedence order determines the precedence of one function over another based on the type of function and its location on the MATLAB path." This order (from highest to lowest) is given below:
The placement of your "method.m" function will likely determine if it gets called or the Java method gets called when using the "method(object,...)" syntax.
Is there a way to ensure I call method.m instead of the Java method?
Right now, I'm guessing your "method.m" is in the current directory or elsewhere on the path (the two lowest precedence positions to be in). If you made "method.m" a subfunction in the larger code calling it, or if it's possible to put it in a private directory where it can be called by every function that needs to call it, then it may get called instead of the Java method when you use the "method(object,...)" syntax.
Hope this helps!
嗯……您可以尝试使用
@method
获取函数句柄,然后调用feval()
在函数句柄上。这可能有用,但我不确定......
Hmmmmmmmmm.... you could try obtaining a function handle using
@method
and then callfeval()
on the function handle.That might work but I'm not sure....