How to call MWNumericArray.toString() from Java?
12 views (last 30 days)
Show older comments
The following Java code is an attempt to implement the test code given in the documentation for MWNumericArray(java.lang.Object rVal, MWClassID classid) given here.
import com.mathworks.toolbox.javabuilder.*;
public class MWArrayError {
public static void main(String[] args) {
double[][] AData = {{ 1, 2, 3, 4, 5, 6},
{ 7, 8 , 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}};
MWNumericArray A = new MWNumericArray(AData, MWClassID.SINGLE);
System.out.println("Array A = \n" + A);
}
}
The (implicit) call to A.toString() in the last line results in the error...
Exception in thread "main" java.lang.RuntimeException: Raised from C++ code using jproxy::throwNamedException
at com.mathworks.toolbox.javabuilder.internal.MWMCR.mxArrayToString(Native Method)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.access$1500(MWMCR.java:33)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mxArrayToString(MWMCR.java:960)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$2.invoke(MWMCR.java:785)
at com.sun.proxy.$Proxy0.mxArrayToString(Unknown Source)
at com.mathworks.toolbox.javabuilder.MWBuiltinArray.toString(MWBuiltinArray.java:180)
at com.mathworks.toolbox.javabuilder.MWNumericArray.toString(MWNumericArray.java:22)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at MWArrayError.main(MWArrayError.java:15)
Could you please tell me how to fix my code to avoid this error? FYI, the call to toString() works for me when A represents a real scalar or single complex value.
0 Comments
Answers (1)
surya venu
on 15 Apr 2024
Hi,
The error you are encountering when calling "toString()" on a "MWNumericArray" object in your Java code is due to the way the "MWNumericArray" class handles multidimensional arrays. To avoid this error and correctly display the contents of your array, you can iterate over the elements of the array and build a string representation manually. Here's how you can modify your code to achieve this:
import com.mathworks.toolbox.javabuilder.*;
public class MWArrayError {
public static void main(String[] args) {
double[][] AData = {{ 1, 2, 3, 4, 5, 6},
{ 7, 8 , 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}};
MWNumericArray A = new MWNumericArray(AData, MWClassID.SINGLE);
StringBuilder sb = new StringBuilder();
sb.append("Array A = \n");
for (int i = 1; i <= A.numberOfElements(); i++) {
sb.append(A.get(i) + " ");
if (i % A.getDimensions()[0] == 0) {
sb.append("\n");
}
}
System.out.println(sb.toString());
}
}
In this modified code snippet, we create a "StringBuilder" to construct the string representation of the array manually. By iterating over the elements of the "MWNumericArray" and appending them to the "StringBuilder", we can avoid the error caused by the implicit call to "toString()" on a multidimensional array. This approach allows you to display the contents of your array in a readable format without encountering the runtime exception.
Hope it helps.
0 Comments
See Also
Categories
Find more on Get Started with MATLAB Compiler SDK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!