作者:金良([email protected]) csdn博客:http://blog.csdn.net/u012176591
C:\Program Files\Java\jdk1.6.0_17\bin;C:\Program Files\Java\jdk1.6.0_17\jre\bin;
java.lang.UnsatisfiedLinkError: Failed to find the library mclmcrrt8_1.dll, required by MATLAB Builder JA, on java.library.path. This library is typically installed along with MATLAB or the MCR, its absence may indicate an issue with that installation or the current path configuration. The MCR version that this component is trying to use is: 8.1. at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ProxyLibraryDir.get( MCRConfiguration.java:162) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ProxyLibraryDir.<clinit>( MCRConfiguration.java:168) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getProxyLibraryDir( MCRConfiguration.java:173) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.get( MCRConfiguration.java:74) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.<clinit>( MCRConfiguration.java:84) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getMCRRoot( MCRConfiguration.java:89) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ModuleDir.<clinit>( MCRConfiguration.java:63) at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getModuleDir( MCRConfiguration.java:68) at com.mathworks.toolbox.javabuilder.internal.MWMCR.<clinit>( MWMCR.java:1462) at plotdemo.PlotdemoMCRFactory.newInstance( PlotdemoMCRFactory.java:50 ) at plotdemo.PlotdemoMCRFactory.newInstance( PlotdemoMCRFactory.java:61 ) at plotdemo.plotter.main( plotter.java:115 )
function drawplot(x, y) plot(x, y);
package jinliang.javaMatlab; import ExampleMatlabLib.PlotExampleMatlab; /* Necessary package imports */ import com.mathworks.toolbox.javabuilder.*; /* * PlotExample class demonstrates plotting x-y data into * a MATLAB figure window by graphing a simple parabola. */ public class PlotExample { public static void main(String[] args) { MWNumericArray x = null; /* Array of x values */ MWNumericArray y = null; /* Array of y values */ PlotExampleMatlab plotExampleMatlab = null; /* Plotter class instance */ int n = 15; /* Number of points to plot */ try { /* Allocate arrays for x and y values */ int[] dims = {1, n};//维度设置一行n列 x = MWNumericArray.newInstance(dims,MWClassID.DOUBLE, MWComplexity.REAL); y = MWNumericArray.newInstance(dims,MWClassID.DOUBLE, MWComplexity.REAL); /* Set values so that y = x^2 */ for (int i = 1; i <= n; i++) { x.set(i, i); y.set(i, i*i); } byte [] rx = x.getByteData();//将MWNumericArray转换成以为数组 for(int i=0;i<rx.length;i++) { System.out.print(rx[i]); System.out.print(" "); } /* Create new plotter object */ plotExampleMatlab = new PlotExampleMatlab(); /* Plot data */ plotExampleMatlab.drawplot(x, y); plotExampleMatlab.waitForFigures(); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } finally { /* Free native resources */ MWArray.disposeArray(x); MWArray.disposeArray(y); if (plotExampleMatlab != null) plotExampleMatlab.dispose(); } } }
function [L] = cholesky(A) L = chol(A); disp(L)Java文件
package jinliang.javaMatlab; import ExampleMatlabLib.MatrixMathExampleMatlab; import com.mathworks.toolbox.javabuilder.*; public class MatrixMathExample { public static void main(String[] args) throws MWException { MWNumericArray mat = null; /* Stores matrix to factor */ Object[] result = null; /* Stores the result */ MatrixMathExampleMatlab theFactor = new MatrixMathExampleMatlab(); /* Stores factor class instance */ try { int n=3; int[] dims = {n, n}; double[][] data = {{2,-1,0},{-1,2,-1},{0,-1,2}}; mat = MWNumericArray.newInstance(dims,MWClassID.DOUBLE, MWComplexity.REAL); /* Set matrix values */ int[] index = {1, 1}; for (index[0]= 1; index[0] <= dims[0]; index[0]++) { for (index[1] = 1; index[1] <= dims[1]; index[1]++) { mat.set(index,data[index[0]-1][index[1]-1]); } } /* Print original matrix */ System.out.println("Original matrix:"); System.out.println(mat); /* Compute cholesky factorization and print results. */ result = theFactor.cholesky(1, mat); System.out.println("Cholesky factorization:"); System.out.println(result[0]); //double[] a = (double [])((MWArray)result[0]).getData(); //for(int i=0;i<a.length;i++) // System.out.println(a[i]); MWArray.disposeArray(result); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } finally { /* Free native resources */ MWArray.disposeArray(mat); MWArray.disposeArray(result); if (theFactor != null) theFactor.dispose(); } } }
function [L,U] = ludecomp(A) [L,U] = lu(A);
package jinliang.javaMatlab; import ExampleMatlabLib.MatrixMathExampleMatlab; import com.mathworks.toolbox.javabuilder.*; public class MatrixMathExample { public static void main(String[] args) throws MWException { MWNumericArray mat = null; /* Stores matrix to factor */ Object[] result = null; /* Stores the result */ MatrixMathExampleMatlab theFactor = new MatrixMathExampleMatlab(); /* Stores factor class instance */ try { int n=3; int[] dims = {n, n}; double[][] data = {{2,-1,0},{-1,2,-1},{0,-1,2}}; mat = MWNumericArray.newInstance(dims,MWClassID.DOUBLE, MWComplexity.REAL); /* Set matrix values */ int[] index = {1, 1}; for (index[0]= 1; index[0] <= dims[0]; index[0]++) { for (index[1] = 1; index[1] <= dims[1]; index[1]++) { mat.set(index,data[index[0]-1][index[1]-1]); } } /* Compute LU factorization and print results. */ result = theFactor.ludecomp(2, mat); System.out.println("LU factorization:"); System.out.println("L matrix:"); System.out.println(result[0]); System.out.println("U matrix:"); System.out.println(result[1]); MWArray.disposeArray(result); } catch (Exception e) { System.out.println("Exception: " + e.toString()); } finally { /* Free native resources */ MWArray.disposeArray(mat); MWArray.disposeArray(result); if (theFactor != null) theFactor.dispose(); } } }
byte [] rx = x.getByteData();//将MWNumericArray转换成以为数组 for(int i=0;i<rx.length;i++) { System.out.print(rx[i]); System.out.print(" "); }把一维的MWNumericArray转换成了一维byte数组。
double[] a = (double [])((MWArray)result[0]).getData(); for(int i=0;i<a.length;i++) System.out.println(a[i]);把一个3*3的MWArray转换成了一维的double []。
本博客代码见 http://download.csdn.net/detail/u012176591/8426473
MATLAB自带的例子目录 D:\Program Files\Matlab\toolbox\javabuilder\Examples