| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Builder JA |
| Contents | Index |
| Learn more about MATLAB Builder JA |
| On this page… |
|---|
As with any Java class, you need to instantiate the classes you create with the MATLAB Builder JA product before you can use them in your program.
Suppose you build a component named MyComponent with a class named MyClass. Here is an example of creating an instance of the MyClass class:
MyClass ClassInstance = new MyClass();
The following Java code shows how to create an instance of a class that was built with MATLAB Builder JA. The application uses a Java class that encapsulates a MATLAB function, myprimes.
/*
* usemyclass.java uses myclass
*/
/* Import all com.mathworks.toolbox.javabuilder classes */
import com.mathworks.toolbox.javabuilder.*;
/* Import all com.mycompany.mycomponent classes */
import com.mycompany.mycomponent.*;
/*
* usemyclass class
*/
public class usemyclass
{
/** Constructs a new usemyclass */
public usemyclass()
{
super();
}
/* Returns an array containing the primes between 0 and n */
public double[] getprimes(int n) throws MWException
{
myclass cls = null;
Object[] y = null;
try
{
cls = new myclass();
y = cls.myprimes(1, new Double((double)n));
return (double[])((MWArray)y[0]).getData();
}
catch (MWException e) {
// something went wrong while
// initializing the component - the
// MWException's message contains more information
}
finally
{
MWArray.disposeArray(y);
if (cls != null)
cls.dispose();
}
}
}
The import statements at the beginning of the program import packages that define all the classes that the program requires. These classes are defined in javabuilder.* and mycomponent.*; the latter defines the class myclass.
The following statement instantiates the class myclass:
cls = new myclass();
The following statement calls the class method myprimes:
y = cls.myprimes(1, new Double((double)n));
The sample code passes a java.lang.Double to the myprimes method. The java.lang.Double is automatically converted to the double data type required by the encapsulated MATLAB myprimes function.
When myprimes executes, it finds all prime numbers between 0 and the input value and returns them in a MATLAB double array. This array is returned to the Java program as an MWNumericArray with its MWClassID property set to MWClassID.DOUBLE.
The myprimes method encapsulates the myprimes function.
The code for myprimes is as follows:
function p = myprimes(n)
% MYPRIMES Returns the primes between 0 and n.
% P = MYPRIMES(N) Returns the primes between 0 and n.
% This file is used as an example for the MATLAB
% Builder for Java product.
% Copyright 2001-2007 The MathWorks, Inc.
if length(n) ~= 1
error('N must be a scalar');
end
if n < 2
p = zeros(1,0);
return
end
p = 1:2:n;
q = length(p);
p(1) = 2;
for k = 3:2:sqrt(n)
if p((k+1)/2)
p(((k*k+1)/2):k:q) = 0;
end
end
p = (p(p>0));
![]() | Importing Classes | Passing Arguments to and from Java | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |