Inconsistent array notation in Java interface generated by MATLAB Compiler SDK?

3 views (last 30 days)
To call Matlab functions from Java, MATLAB's Compiler SDK generates functions with the following signatures.
/* mlx interface - List version*/
public void mysum(List lhs, List rhs)
throws MWException
/* mlx interface - Array version*/
public void mysum(Object[] lhs, Object[] rhs)
throws MWException
/* standard interface - no inputs */
public Object[] mysum(int nargout)
throws MWException
/* standard interface - variable inputs */
public Object[] mysum(int nargout, Object varargin)
throws MWException
Here, `mlx` seems to refer to an signature convention, and speculatively, the reasons for the name seem to be no longer relevant in the current context.
Here is an example invocation from within function getsum:
public double getsum(double[] vals) throws MWException
{
myclass cls = null; // Class containing the generated functions
Object[] x = {vals};
Object[] y = null;
try
{
cls = new myclass();
y = cls.mysum(1, x);
...snip...
"[O]bject array [x] of length 1 is created and initialized with a reference to the supplied double array. This argument is passed to the mysum method. The result is known to be a scalar double..." The invocation matches 4th and last signature above.
Why is argument x of type Object array (Object[] x) when the 4th signature shows a scalar Object (Object varargin)?
I realize that an array itself (i.e., Object[] x) is also an Object, but if that is how the prototypes are to be interpretted, then why is the array nature of the arguments explicit in the 2nd prototype above (Object[] rhs) but not the 4th prototype (Object varargin)?

Accepted Answer

Todd Flanagan
Todd Flanagan on 29 Oct 2020
You are correct that mlx is a convention. It refers to a c style interface that is familiar to people using other c-style MATLAB interfaces.
I don't know the specifics of why the particular signature was chosen and what the design trade offs were, but the signature does expect and Object [] array to be passed in as Object per the documentation.
In all cases, the varargin argument is passed as type Object. This lets you provide any number of inputs in the form of an array of Object, that is Object[], and the contents of this array are passed to the compiled MATLAB function in the order in which they appear in the array.
  6 Comments
FM
FM on 29 Oct 2020
I appreciate the offer, Todd. With the clarity about different notations between standard/mlx interface, your advice on preferring the former, and your example coding of Matlab structs in Java, I'm going to see if I can cobble it together myself.
FM
FM on 30 Oct 2020
Edited: FM on 30 Oct 2020
Just a possible correction to the documentation: "In all cases, the varargin argument is passed as type Object". Of the four signatures, only the 4th contains "varargin". Notwithstanding argument name differences, I suspect that the statement only applies to the the 2nd & 4th signatures. It may apply to the 1st signature, which uses a "List" argument, but I'm not sure whether the ensuing sentence applies: "This lets you provide any number of inputs in the form of an array of Object, that is Object[]...". That depends on whether "List" qualifies as a form of "Object[]". Not being a seasoned veteran in Java, I could very well be wrong, but my OOP gut tells me that an instance of "List" is not an array of something, even though it may use an array internally. I followed List's class ancestory as far as "Iterable" and did not see any indication that List was a form of Object[]: https://docs.oracle.com/javase/8/docs/api/java/util/List.html

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!