Академический Документы
Профессиональный Документы
Культура Документы
LiTH
ABSTRACT
Sometimes MATLABs own programming language and components are insufficent for your programming needs. Using Java classes to extend MATLAB is a great way of doing so. Maybe there is a need to speed up a MATLAB function by writing it in Java or extending a MATLAB program with a database connection. This process description describes the integration of Java classes in MATLAB. It is not a complete description of the opportunities MATLAB offers. It meant to inform you of the possibilites and get you started. More information about communication between MATLAB and Java enviroments can be found by following the links under References.
Field of application
1 Field of application
MATLAB is a widely spread program that is used in many different areas. There is a great need to use Java functions from within MATLAB since there are times when its build-in programming language is insufficient. Extending MATLAB with Java is modern and platform independent solution for this problem. Use this process description to understand how to extend MATLAB with the rich possibilites of the Java language. The MATLAB Java interface enables you to: Acess Java API (application programming interface) class packages that support essential activities such as I/O and networking. For example, the URL class provides convienient access to resources on the internet. Access third-party Java classes Easily construct Java objects in MATLAB Call Java object methods, using either Java or MATLAB syntax Pass data between MATLAB variables and Java objects
2 Prerequisites
The reader is assumed to possess basic knowledge about the programme MATLAB and also the programming language Java. Knowledge about data types and data structures is also necessary. It is assumed that the user works with MATLAB version 6.5 or later. The examples are created and tested with MATLAB version 7.0.
3 Realization
3.1 Overview
Defining Java classes
Java classes to be used in MATLAB can either be created by you, using numeros tools for Java development, standard library classes in the Java language or third party classes. When creating your own classes no adaption to the MATLAB enviroment is necessary. Creating the Java classes is beyond the scope of this document. See references for further reading.
Realization
Edit this file to add and remove classes availible in MATLAB. To avoid changing the classpath of all users using the system, the classpath can be copied to your home directory. When MATLAB starts it will search the current directory for classpath.txt and use it if present. The dynamic path can be changed at runtime, with the commands javaclasspath, javaaddpath and javarmpath. See refereces for further documentation on these commands. The flexibility of changing the classpath at runtime is an advantage comparing to the static path, but the classes are loaded slower than on the static path. Use the dynamic path under development and put the classes in the static path when you decided wich classes to use. Creating Java objects Java objects can be created with the standard Java syntax:
object = classname(constrArg1,..,constrArgn)
The advantage of this method is that the class name can be specified at runtime.
Realization
Using Java and MATLAB calling syntax To call methods on Java objects, you can use the Java syntax:
object.method(arg1,...,argn)
Alternatively, you can call (nonstatic) methods with the MATLAB syntax:
method(object,arg1,..,argn)
Simplifying Java Class Names In MATLAB you can refer to any Java class by its fully qualified name, which include its package name, but a fully qualified name can be rather long and make the code hard to read. Instead you can use the MATLAB import command and then use only the class name without the package prefix. Examples:
import class_name
Imports all classes in the package. Passing data to a Java method When you make a call from MATLAB to Java code, any MATLAB data types passed in the call are converted to data types native to the Java language. MATLAB performs this conversion on each argument that is passed, except for those arguments that are already Java objects. This table below describes the conversion that is performed on specific MATLAB data types.
Realization
Handling data returned from a Java method In many cases, data returned from Java is incompatible with the data types operated on whithin MATLAB. When this is the case, MATLAB converts the returned value to a data type native to the MATLAB language. The types in the table below are converted and all other types (eg. Java objects)are stored in MATLAB in ther original form.
Handling Java Exceptions If a Java method or constructor is invoked, that throws an exception, MATLAB catches the exception and transforms it into a MATLAB error. MATLAB puts the text of the Java error message into its own error message. Receiving an error from a Java method or constructor has the same apperance as receiving an error from an M-file. Determining the Class of an Object To find the class of a java object, use the query form of the MATLAB function class:
classname = class(object)
The classname is returned as a matlab char array. This command works with MATLAB objects as well. To find out if it is a Java object or not, use the isjava function:
objecttype = isjava(object)
Results
Converting Objects to MATLAB data types With the exception of the types listed in Table 2, MATLAB does not convert Java objects returned from method calls to a native MATLAB data type. If you want to convert Java object data to a form useable in MATLAB, there are a few MATLAB functions that enable you to do this. Use the double function in MATLAB to convert any Java object or array of objects to the MATLAB format double. Note that the specified object must inherit from java.lang.number or implement a toDouble function. Otherwise MATLAB will report an error. Use the char function in MATLAB to convert java.lang.String objects to MATLAB character arrays. An array of java.lang.String objects results in a MATLAB cell array containing character arrays. Note that if the object type is not of the java.lang.String class or does not implement a toChar method, MATLAB will report an error. Use the cell function to convert a Java array or Java object into a MATLAB cell array. Element of the resulting cell array will be of the MATLAB type closest to the Java array element or Java object. The syntax for these commands are displayed below:
matlab_double = double(object) matlab_char = char(object) matlab_cell = cell(object)
Saving and loading Java objects to MAT-files Use the MATLAB save function to save a Java object to a MAT-file. Use the load function to load it back into MATLAB from that MAT-file. To save a Java object to a MAT-file make sure that the object and its class implements the serializable interface (part of the Java API), either directly inheriting it from a parent class. Any embedded or otherwise referenced objects must also implement the Serializable interface.
4 Results
4.1 Products
The result is an extension of MATLAB in the form of a MATAB m-file or a set of commands to utilize the new functionality. If you develop the Java classes yourself, these are also part of the result.
%% Set up a buffered stream reader isr = java.io.InputStreamReader(is); br = java.io.BufferedReader(isr); %% Skip initial html formatting lines for k = 1:30 s = readLine(br); end %% Read 8 lines of text c = ''; for k = 1:8 s = readLine(br); %% This is a java.lang.String object %% Concatenate the strings into a MATLAB char array c = strvcat(c, char(s)); end %% Display the result c
handle=MWFrame(title); colwidth = 100; rowheight = 32; [nrows, ncolumns]=size(data); b = MWListbox; b.setColumnCount(ncolumns); for i=1:ncolumns, b.setColumnWidth(i-1 ,colwidth); b.setColumnHeaderData(i-1,data{1,i}); % b.setRowHeight(i-1,rowheight); end; %% Set the behaviour of the table b.setExcelMode(1); ts=b.getTableStyle; ts.setVGridVisible(1); ts.setHAlignment(1); b.setTableStyle(ts); co = b.getColumnOptions; set(co, 'HeaderVisible', 'on'); set(co, 'Resizable', 'on'); b.setColumnOptions(co); %% Fill up the table with data: text={}; for i=2:nrows for j=1:ncolumns text{1,j}=mat2str(data{i,j}); end b.addItem(text); text={}; end b.setSelectedIndex(0); handle.add(b); handle.setBounds(300,300,colwidth*ncolumns,rowheight*nrows); %% Make the window closeable handle.addWindowListener(window.MWWindowActivater(handle)); handle.setVisible(1);
10
13 References
13.1 Method description
MATLAB External Interfaces/API, http://www.mathworks.com/access/helpdesk/help/techdoc/rn/external_new.html. MATLAB External Interfaces/API Reference, http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/apiref.shtml. MATLAB Central/ Utilities, http://www.mathworks.com/matlabcentral/fileexchange/loadCategory.do?objectId=8&objectType=Category Java in a Nutshell, David Flanagan 4th edition 2002
11
References
12