Note that there are some explanatory texts on larger screens.

plurals
  1. POCan MATLAB not read back a double[] array from Java?
    primarykey
    data
    text
    <p>Consider MyClass.java:</p> <pre><code>public class MyClass { public void firstfunction(double fwd[]) { fwd[0] = 42; } public void secondfunction(Double fwd[]) { fwd[0] = new Double(42); } } </code></pre> <p>Both functions return the value 42 in <code>fwd</code>, right?</p> <p>From within MATLAB, I want to access this value 42:</p> <pre><code>myobj=MyClass; var1=0.0; myobj.firstfunction(var1); fprintf('%1.1f',var1); %// ... var1 is still 0.0 ... :-( var2 = javaArray ('java.lang.Double',1); var2(1)=java.lang.Double(0.0); myobj.secondfunction(var2); %// var2 now contains the value 42 :-) </code></pre> <p>While both calls "work" (as is: no error message), only var2 contains the return value 42; var1 still has the value 0.0. </p> <p><strong>Is there any way to use MATLAB to call the function <code>firstfunction</code> and retrieve the return value?</strong></p> <hr> <p><em>Some background:</em> MATLAB can pass Java objects when calling a Java function, and modifications to these objects are afterwards available in MATLAB - <em>except</em> when the Java object is an array of a primitive data type. In this case automatic conversion between MATLAB and Java kicks in, making a Java array-of-primitive-double correspond directly to a double matrix in MATLAB - which is by MATLAB conventions a thing "passed as value" so no return values are possible. So my question can be rephrased as <em>is there any way around this?</em></p> <hr> <p><strong><em>(you can stop reading here.)</em></strong></p> <p>For reference, my special case was this:</p> <p>I have a <strong>Java class</strong> MyClass.java wrapping a DLL, which I want to use in MATLAB. However, the return value of one of the functions is a <strong>double[] passed as a parameter</strong>, the content of which <strong>doesn't make it back to MATLAB</strong> due to how interaction with Java is implemented.</p> <p>Is there any way around this problem, without modifying the way the DLL returns the data?</p> <p>Here are the ugly details:</p> <pre><code>public class MyClass { static { System.load("C:\\fullpath\\mydll.dll"); } public static native long SetFWD(double fwd); public static native long GetFWD(double fwd[]); } </code></pre> <p>This is visible from within MATLAB once I set the javapath correctly:</p> <pre><code>&gt;&gt; methods MyClass -full Methods for class MyClass: static long GetFWD(double[]) MyClass() static long SetFWD(double) [and stuff inherited from java.lang.Object] </code></pre> <p>I can call the function SetFWD from within MATLAB, but I can't get GetFWD to return anything:</p> <pre><code>myobj=MyClass; fwd=3.0; myobj.SetFWD(fwdval); % this works fine fwd=0.0; myobj.GetFWD(fwd); % this does not give an error, but fwd stays unmodified - as one would expect in MATLAB fwd = javaArray ('java.lang.Double',1); fwd(1) = java.lang.Double(0.0); myobj.GetFWD(fwd) % this gives the error "??? No method 'GetFWD' with matching signature found for class 'MyClass'." </code></pre> <p>From reading MATLAB Documentation <a href="http://www.mathworks.com/help/techdoc/matlab_external/f6425.html" rel="nofollow noreferrer">Passing Data to a Java Method</a> and <a href="http://www.mathworks.com/help/techdoc/matlab_external/f15351.html" rel="nofollow noreferrer" title="Working with Java Arrays">Working with Java Arrays</a> as well as SO posts <a href="https://stackoverflow.com/questions/4869615/moving-from-java-types-back-to-matlab-types">Moving from Java types back to MATLAB types</a> and <a href="https://stackoverflow.com/questions/464216/strange-classes-passed-from-matlab-to-java">Strange classes passed from matlab to java</a>, I understand that Matlab automagically converts any double array that I pass to the function into a Java array, and then ignores whatever modifications does in these arrays. It seems that if my function definition in MyClass contained Double objects instead of double primitives, my second attempt could work.</p> <p>Is there any way to get MATLAB to return the value I'm after, without modifying the original .DLL (mydll.dll)?</p> <p><em>Update</em></p> <p>I understand that MATLAB usually passes everything "by value". But in <a href="http://www.mathworks.com/help/techdoc/matlab_external/f6425.html" rel="nofollow noreferrer">Passing Data to a Java Method</a> Mathworks say that</p> <blockquote> <p>If you need to access changes that a Java method makes to an array, then, rather than passing a MATLAB array, you should create and pass a Java array, which is a reference.</p> </blockquote> <p>They explain in <a href="http://www.mathworks.com/help/techdoc/matlab_external/f15351.html" rel="nofollow noreferrer" title="Working with Java Arrays">Working with Java Arrays</a> how to do that using the javaArray function, but I couldn't get this to work for creating an array double[] (i.e. an array of primitive doubles), only for Double[] (i.e. an array of Double objects) which is not what I need here, since my function GetFWD() doesn't eat the latter :-(.</p> <pre><code>&gt;&gt; A=javaArray ('java.lang.double',1); % works fine, but cannot be used as parameter for my function GetFWD (see "No Method ... with matching signature..." error above) &gt;&gt; A=javaArray ('double',1); ??? Error using ==&gt; javaArray No class double can be located on the MATLAB Java classpath </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload