[英]Return Array from void Method
我是Java新手,無法解決我遇到的范圍問題。 我有一個Matlab Connect( MConnect1()/MConnect2()
)方法,該方法必須為void
。 我試圖從另一個類訪問方法中的變量,但得到NullPointerException
。 我嘗試將方法創建為getResult()
但這也不起作用。 我應該走哪一步?
我正在嘗試從void MConnect2()獲取array1。
package OMatlab;
import matlabcontrol.*;
import matlabcontrol.extensions.*;
import java.util.Arrays;
public class MatlabConnectVariable extends MatlabProxyFactory{
private String MConnectStatus;
public double result1;
public String result2;
public double[][] array1;
public double[][] array2;
public int i;
public boolean conStat;
public String getMConnectStatus() {
return MConnectStatus;
}
//Default Constructor
MatlabConnectVariable()
{
if (conStat = true) {
MConnectStatus="The MatlabConnect class instance has been initiated";}
else {MConnectStatus="WARNING: The MatlabConnect class instance could not been initiated";}
}
public void MConnect1() throws MatlabConnectionException, MatlabInvocationException
{
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.setHidden(true)
.setMatlabLocation(null).build();
// MatlabProxyFactoryOptions options =
// new MatlabProxyFactoryOptions.Builder()
// .setUsePreviouslyControlledSession(true)
// .build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
//MatlabProxyFactory factory = new MatlabProxyFactory();
// create proxy
MatlabProxy proxy = factory.getProxy();
conStat = proxy.isConnected();
// ////Set a variable, add to it, retrieve it, and print the result
// proxy.setVariable("a", 5);
// proxy.eval("a = a + 6");
// result = ((double[]) proxy.getVariable("a"))[0];
// System.out.println("Result: " + result);
//call user-defined function (must be on the path)
//proxy.eval("addpath('C:\\Users\\odogu1\\Documents\\MATLAB')");
proxy.feval("data_loading_script");
//proxy.eval("rmpath('C:\\Users\\odogu1\\Documents\\MATLAB')");
proxy.setVariable("a", 1);
result1 = ((double[]) proxy.getVariable("new_data_added"))[0];
// close connection
proxy.disconnect();
}
public void MConnect2() throws MatlabConnectionException, MatlabInvocationException
{
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.setHidden(true)
.setMatlabLocation(null).build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
//MatlabProxyFactory factory = new MatlabProxyFactory();
// create proxy
MatlabProxy proxy = factory.getProxy();
conStat = proxy.isConnected();
//call user-defined function (must be on the path)
// proxy.feval("data_loading_script");
//Create a 4x3 array filled with random values
proxy.eval("array1 = randn(4,1)");
//Print a value of the array into the MATLAB Command Window
proxy.eval("disp(['entry: ' num2str(array1(1, 1))])");
//Get the array from MATLAB
MatlabTypeConverter processor = new MatlabTypeConverter(proxy);
double[][] array1 = processor.getNumericArray("array1").getRealArray2D();
//Print out the same entry, using Java's 0-based indexing
// System.out.println("entry: " + array1.getRealValue(2, 1));
//Convert to a Java array and print the same value again
// double[][] result2 = array1.getRealArray2D();
// System.out.println("ENTRY: " + result2[0][0]);
int p=0;
for(int i = 0; i < array1.length; i++)
{
System.out.println(Arrays.toString(array1[i]));
// result2 = Arrays.toString(array1[i]);
}
// System.out.println(Arrays.toString(result2));
// proxy.setVariable("a", 110);
// result2 = ((double[]) proxy.getVariable("a"))[0];
//result2 = ((double[]) proxy.getVariable("new_data"));
// close connection
proxy.disconnect();
}
public double[][] getResult2() {
return array1;
}
}
看起來您已將數組定義為字段,但從未使用過。 您在該方法中用以下幾行定義了另一個同名:
MatlabTypeConverter processor = new MatlabTypeConverter(proxy);
double[][] array1 = processor.getNumericArray("array1").getRealArray2D();
因此,您不會讓array1充滿任何數據。 做就是了:
array1 = processor.getNumericArray("array1").getRealArray2D();
並嘗試調用getResult2()。
在方法getResult2()中,您將返回定義為類成員的array1,而您無需對其進行初始化(因此,總是將其設為null)。 你應該換線
double[][] array1 = processor.getNumericArray("array1").getRealArray2D();
進入
array1 = processor.getNumericArray("array1").getRealArray2D();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.