Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4260936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:59:13+00:00 2026-05-21T05:59:13+00:00

Consider MyClass.java: public class MyClass { public void firstfunction(double fwd[]) { fwd[0] = 42;

  • 0

Consider MyClass.java:

public class MyClass {
  public void firstfunction(double fwd[]) {
   fwd[0] = 42;
  }
  public void secondfunction(Double fwd[]) {
   fwd[0] = new Double(42);
  }
}

Both functions return the value 42 in fwd, right?

From within MATLAB, I want to access this value 42:

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  :-)

While both calls “work” (as is: no error message), only var2 contains the return value 42; var1 still has the value 0.0.

Is there any way to use MATLAB to call the function firstfunction and retrieve the return value?


Some background: MATLAB can pass Java objects when calling a Java function, and modifications to these objects are afterwards available in MATLAB – except 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 is there any way around this?


(you can stop reading here.)

For reference, my special case was this:

I have a Java class MyClass.java wrapping a DLL, which I want to use in MATLAB. However, the return value of one of the functions is a double[] passed as a parameter, the content of which doesn’t make it back to MATLAB due to how interaction with Java is implemented.

Is there any way around this problem, without modifying the way the DLL returns the data?

Here are the ugly details:

public class MyClass
{
    static
    {
        System.load("C:\\fullpath\\mydll.dll");
    }
public static native long   SetFWD(double fwd);
public static native long   GetFWD(double fwd[]);
}

This is visible from within MATLAB once I set the javapath correctly:

>> methods MyClass -full

Methods for class MyClass:

static long GetFWD(double[])
MyClass()
static long SetFWD(double)
[and stuff inherited from java.lang.Object]

I can call the function SetFWD from within MATLAB, but I can’t get GetFWD to return anything:

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

From reading MATLAB Documentation Passing Data to a Java Method and Working with Java Arrays as well as SO posts Moving from Java types back to MATLAB types and Strange classes passed from matlab to java, 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.

Is there any way to get MATLAB to return the value I’m after, without modifying the original .DLL (mydll.dll)?

Update

I understand that MATLAB usually passes everything “by value”. But in Passing Data to a Java Method Mathworks say that

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.

They explain in Working with Java Arrays 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 :-(.

>> 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)
>> A=javaArray ('double',1);
??? Error using ==> javaArray
No class double can be located on the MATLAB Java classpath
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:59:13+00:00Added an answer on May 21, 2026 at 5:59 am

    No, it can not.

    I have found this workaround, that I can live with: while I can’t (myself) modify the DLL, I can add a wrapper function in the java class MyClass, like so:

    public static long  GetFWDwrp(Double fwd[]) {
        double tmp[]=new double[1];
        long retval = MyClass.GetFWD(tmp);      
        fwd[0] = tmp[0];
        return retval;
    }
    

    This wrapper function has an array of Double objects as a parameter, which I can access from MATLAB like this:

    oldFW = javaArray ('java.lang.Double',1);
    oldFW(1)=java.lang.Double(0.0);
    myobj.GetFWDwrp(oldFW);
    oldFW % this now contains the return value
    

    So this answers my question for a workaround to the problem, since it doesn’t involve changing the interface of the DLL (only the interface of the Java Class).

    However, the more fundamental question in the title is still unanswered: Is it really impossible in MATLAB to pass a reference to an array-of-primitive-double to a Java function, circumventing the automatic conversion of Matlab-double-array to Java-primitive-double-array and back which seems to make it impossible to access any changes the Java code made to such an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code snippet: class MyClass{ private List myList; //... public List getList(){ return
Consider the following class template: template <class T> class MyClass { void MyFunc(); };
Consider the following: namespace MyNamespace{ class MyClass { public: // Public area private: //
Consider these classes. class Base { ... }; class Derived : public Base {
Consider the following code: class myclass { function __construct(&$arg1, &$arg2) { echo $arg1; echo
Consider this code : class MyClass<T> { } class AnotherClass : MyClass<String> { }
Consider this example: class MyClass: def func(self, name): self.name = name I know that
Consider: class MyClass<T> where T : class { } In that case, the where
Consider two extension methods: public static T MyExtension<T>(this T o) where T:class public static
Consider the following code: class MyClass { template <typename Datatype> friend MyClass& operator<<(MyClass& MyClassReference,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.