Project

General

Profile

Actions

Morph-M Matlab - How to use Morph-M with Matlab » History » Revision 5

« Previous | Revision 5/26 (diff) | Next »
Serge Koudoro, 12/29/2008 01:13 PM


How to use Morph-M with Matlab

Introduction

The capabilities of Matlab can be extended through domain specific software packages called Matlab toolboxes. A typical Matlab toolbox is a directory structure which contains new data structures and function definitions that extend Matlab's capabilities to a new science or engineering area. A Matlab toolbox user simply copies the toolbox directory structure and tells Matlab where the toolbox is located. The toolbox defined functions and datatypes then become available from Matlab.

Setting up Java-Wrapped Morph-M in Matlab

Java is very extensively supported in Matlab. Since we have Java wrappers in VTK, we can quickly get access to all of VTK in Matlab using the Java wrappers.

Overcome Matlab loadLibrary bug

Matlab has a bug with newer Java versions where you cannot explicitly call loadLibrary() from the Matlab prompt (see [1]). To get around this, we need to provide a little utility which just calls this function. Write the following code in a file LoadLibrary.java, save it, then compile with "javac LoadLibrary.java":
class LoadLibrary { public static void loadLibrary(String s) { java.lang.System.loadLibrary(s); } }
Side note: The only solution posted on their site had been to use an earlier version of Java. I submitted this workaround, which is now part of their solution. You're welcome, MathWorks :) Jeff 10:58, 26 August 2008 (EDT)
[edit]
Matlab configuration

We need to tell Matlab where to find the Java class files for VTK and the Matlab load library work-around.

This can be done dynamically from the Matlab command prompt, but these commands will need to be applied each time Matlab is started:

>> javaaddpath('insert-your-path-to-the-VTK-build-directory-here/bin/vtk.jar')
>> javaaddpath('insert-your-path-to-the-directory-containing-LoadLibrary.class-here');

There are two alternatives for a permanent addition to the Matlab Java class path. The first method requires root or administrative access to the Matlab installation on your system (if Matlab is installed in your local Unix directory you can also use this method). This change will apply to all users who access this Matlab installation. The second method is to modify a local copy of the classpath.txt file in the local users Matlab startup directory.

For method one: as root (Unix) or as an account with administrator priveledges (Windows), execute the following from within Matlab, or directly edit the classpath.txt file with any text editor:

edit([matlabroot '/toolbox/local/classpath.txt']);

For method two: make a copy of $matlabroot/toolbox/local/classpath.txt into your Matlab startup directory and edit that copy (on Unix the Matlab startup directory is in your home directory under .matlab/R2008a).

Add the following lines to your classpath.txt (system or local copy):

insert-your-path-to-the-VTK-build-directory-here/bin/vtk.jar
insert-your-path-to-the-directory-containing-LoadLibrary.class-here

Next, we need to tell Matlab where to find the dynamic libraries for VTK by editing librarypath.txt. Again, there are two options. Option one requires root or adiminstrator priveledges and changes the Matlab search path for all users of the Matlab installation. Options two modifies a local copy of librarypath.txt file and applies to the local user.

For method one: as root (Unix) or as an account with administrator piveledgles (Windows), execute the following from within Matlab, or directly edit the librarypath.txt file with any text editor:

edit([matlabroot '/toolbox/local/librarypath.txt']);

For method two: make a copy of $matlabroot/toolbox/local/librarypath.txt your Matlab startup directory and edit that copy (on Unix the Matlab startup directory is in your home directory under .matlab/R2008a).

Add the following line to librarypath.txt (on Windows you would append "/Debug" or "/Release" to obtain the path containing the VTK libraries)

insert-your-path-to-the-VTK-build-directory-here/bin

Using Morph-M within Matlab

Now the part we've all been waiting for. In order to use VTK stuff in Matlab, first execute

LoadLibrary.loadLibrary('vtkViewsJava');

This should load vtkViews and all its dependencies (which should be everything in VTK). Now we are ready to go. Just execute anything you could write in e.g. Python wrappers, such as:

g = vtk.vtkMutableDirectedGraph();
g.AddVertex();
g.AddVertex();
g.AddGraphEdge(0, 1);

Settings Compabilities problems

h2{color:#8B0000}.Matlab Titan Toolbox Release

Updated by Serge Koudoro about 15 years ago · 5 revisions