Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

JBoost – Integrating an external Java library in Matlab

June 29, 2011 No Comments

I would like to welcome guest blogger Adam Chapman, who has recently started a dedicated Matlab blog at http://intelilab.blogspot.com/. Today, Adam shares his experience in integrating a 3rd-party Java library – in this case, the open-source JBoost library, in a Matlab application.
I had received a couple of requests asking me to create alternating decision trees (ADTs) in Matlab. Due to the self-organising nature of an ADT structure, it would be an extremely slow and frustrating task to code up an ADT building algorithm in any language.
I was cautious of looking for source code that would do the job for me, as I was concerned that interfacing external code with Matlab would be another difficult task.
I had interfaced external VBA and C++ code with Matlab before, but never attempted interfacing Java code. I remember all of those previous interfacing projects being a nightmare to debug and very tricky to code into a stable solution. After some hesitation I decided that I would give the open-source Jboost library a try.
It turns out that “every Matlab installation includes a Java Virtual Machine (JVM), so that you can use the Java interpreter via MATLAB commands, and you can create and run programs that create and access Java objects”.
That’s all very well, but I needed to know exactly how I should call the main() function in Jboost directly from Matlab. In this Article I’ll explain how I replicated the example walkthrough on the Jboost website, using the spambase data set as an example.
Reading the Jboost installation notes, I found that three paths need to be added to the Java class path. Instead of adding a system environment variable like the Jboost documentation suggests, we can simply use the Matlab’s built-in javaaddpath function:

JBOOST_DIR='C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2';
newpath1=[JBOOST_DIR '\dist\jboost.jar'];
newpath2=[JBOOST_DIR '\lib\jfreechart-1.0.10.jar'];
newpath3=[JBOOST_DIR '\lib\jcommon-1.0.8.jar'];
% add Jboost classes to javaclasspath
javaaddpath(newpath1)
javaaddpath(newpath2)
javaaddpath(newpath3)
% also add JBOOST_DIR to the matlab path so that matlab can look there for non-jave code and data files:
addpath(JBOOST_DIR)

JBOOST_DIR='C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2'; newpath1=[JBOOST_DIR '\dist\jboost.jar']; newpath2=[JBOOST_DIR '\lib\jfreechart-1.0.10.jar']; newpath3=[JBOOST_DIR '\lib\jcommon-1.0.8.jar']; % add Jboost classes to javaclasspath javaaddpath(newpath1) javaaddpath(newpath2) javaaddpath(newpath3) % also add JBOOST_DIR to the matlab path so that matlab can look there for non-jave code and data files: addpath(JBOOST_DIR)

we can check those paths were added by reading back the dynamic java classpath:

>> javaclasspath('-dynamic')
DYNAMIC JAVA PATH
C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\lib\jcommon-1.0.8.jar
C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\lib\jfreechart-1.0.10.jar
C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\dist\jboost.jar

>> javaclasspath('-dynamic') DYNAMIC JAVA PATH C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\lib\jcommon-1.0.8.jar C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\lib\jfreechart-1.0.10.jar C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\dist\jboost.jar

Now I had the paths set up for Matlab and the JVM to find code and files in, I had to work out how to actually run the Jboost main() function.
Matlab’s methodsview function lists information on the methods in a Java class:

methodsview('jboost.controller.Controller')

methodsview('jboost.controller.Controller')

pops a new window, from which we can derive that the main function for Jboost expects a java.lang.String array object (which is standard for a main() function in Java):

View of JBoost's class function using methodsview (click for details)
View of JBoost's class function using methodsview (click for details)

(we could also have used Yair’s UIINSPECT utility on the File Exchange, to display even more information on the Java class’s methods, callbacks and properties, but for our simple usage here, the built-in methodsview function is sufficient)
To build a Java string array to input to Jboost, we use the built-in java_array function:

strArray = java_array('java.lang.String', 1);
strArray(1) = java.lang.String('-S');
strArray(2) = java.lang.String('C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\spambase');
strArray(3) = java.lang.String('-CONFIG');
strArray(4) = java.lang.String('spambase.config');
strArray(5) = java.lang.String('-m');
strArray(6) = java.lang.String(‘MATLABTREE.m');

strArray = java_array('java.lang.String', 1); strArray(1) = java.lang.String('-S'); strArray(2) = java.lang.String('C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\spambase'); strArray(3) = java.lang.String('-CONFIG'); strArray(4) = java.lang.String('spambase.config'); strArray(5) = java.lang.String('-m'); strArray(6) = java.lang.String(‘MATLABTREE.m');

Note how I initialized it with a size of 1 element, and let it grow as new elements are added since the array is very small there is no need to worry about the time saved by pre-allocating. The first 2 elements define the directory that Jboost is to look for data files in, the next 2 define a standard Jboost configuration file and the final 2 elements are rather special: they define the name of the m-file that will contain automatically generated Matlab code, allowing us to use the decision tree in the future without going in and out of Java.
Alternately, we could have simply used Matlab’s cell-array, as in:

strArray = {'-S', 'C:\...', '-CONFIG', 'spambase.config', '-m', 'MATLABTREE.m'};

strArray = {'-S', 'C:\...', '-CONFIG', 'spambase.config', '-m', 'MATLABTREE.m'};

Now to construct an Alternating Decision Tree with Jboost, just call the Java jboost class containing the main() function, and input the array of parameters that we just constructed:

% input the configuration class into the jboost controller
jboost.controller.Controller.main(strArray)

% input the configuration class into the jboost controller jboost.controller.Controller.main(strArray)

With all the code so far, Matlab will invoke the Java jboost libraries to train an ADT to detect spam emails based on their contents defined in the spambase dataset. You will see the verbose java print-outs in the Matlab command window while it is running.
The jBoost developers use a Perl script to create an image of the completed ADT. That is even easier to call from Matlab than the Java routines were:

perl('atree2dot2ps_hi_res.pl','-d','C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\','-s','spambase.spec','-i','spambase.info','-t','spambase.output.tree')

perl('atree2dot2ps_hi_res.pl','-d','C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\','-s','spambase.spec','-i','spambase.info','-t','spambase.output.tree')

Now if you look in the folder that you defined in the “-d” parameter above, you will see that the Perl script has created a few images of the tree in different formats. The .png file is below. I think it is incredible that an inanimate machine can be programmed to have the intelligence to create something so intricate.

Resulting ADT tree (click for details)
Resulting ADT tree (click for details)

During this small project I realized that calling Java and Perl classes from Matlab is actually pretty easy. However, calling python, which jboost uses for some post-processing routines is actually very hard. I’ve decided to create my own Matlab versions of the error and margin plotting functions that Jboost uses, and will add them to the sourceforge project as soon as I’m happy with them.
I’d like to thank Aaron Arvey and Yair Altman for their friendly support during this project.

Related posts:

  1. Matlab-Java memory leaks, performance – Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...
  2. Converting Java vectors to Matlab arrays – Converting Java vectors to Matlab arrays is pretty simple - this article explains how....
  3. Assessing Java object size in Matlab – Java object sizes are not reported by Matlab, but we can still estimate them using two free external utilities. ...
  4. Matlab-Java interface using a static control – The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....
  5. JMI – Java-to-Matlab Interface – JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....
  6. Using Java Collections in Matlab – Java includes a wide selection of data structures that can easily be used in Matlab programs - this article explains how. ...
Adam Chapman Java
Print Print
« Previous
Next »
Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Yair Altman (8 days 2 hours ago): Robot only runs when you tell it to run a command such as keyPress. If you don’t tell it to run a command, it uses no CPU, so there’s no need to remove the Robot...
  • Eric (8 days 13 hours ago): Hey @Kevin, can you share your code about create group of figures in the AppContainer? The container of multiples uifigures could be an amazing improvement over AppDesigner and its...
  • Elsa Smith (9 days 4 hours ago): I recently used java.awt.Robot to perform GUI testing on MATLAB and found it to be an extremely easy and useful way to control mouse movements.
  • Elsa Smith (9 days 4 hours ago): I’m suspecting that the slow performance of my GUI may be due to the use of java.awt.Robot. Is there a way to cancel/stop/remove the robot after it has been created, or is...
  • Michelle Kline (9 days 21 hours ago): *edit* tip about fopen(), not about fwrite(). ‘Wb’ vs. ‘wb’
  • Michelle Kline (9 days 21 hours ago): Thank you, Yair! With this previously-unknown-to-me tip about fwrite() performance, you have saved me literally hours of processing time. Michelle Kline Department of...
  • Alessandro Beda (22 days 9 hours ago): I found what I think is a bug related to this (tested in R2022 and R2023a). If I add a “ButtonDownFcn” to the plots (see example below), then the modified...
  • Nicholas (24 days 0 hours ago): Yair, Changing the desktop help options did not solve the issue. Though, it’s unclear how I could change these options in the Runtime, if that’s what you meant? I should...
  • Yair Altman (27 days 19 hours ago): @Francisco – this is one of those cases where you should ask MathWorks support. After all, you’re trying to use a supported Matlab functionality when you encountered...
  • Francisco Campos (28 days 7 hours ago): Hello, thanks for all your work that has been immensely useful for those working in the Matlab environment. I have been trying to replace matlabcontrol with the official...
  • Yair Altman (32 days 7 hours ago): Kei – this is possible, I believe that I saw this ability somewhere, a few years ago. I don’t remember exactly where, it will require a bit of research, but...
  • Kei (32 days 10 hours ago): Hello Yair Thank you for this great article. I would like to freeze first two columns in uitable. Do you know if such option is available? Since looks like this option is not available...
  • Andrés Aguilar (35 days 21 hours ago): Hello, has anyone tried to change the language of the DateComboBox? For example English -> French ————&# 8212;—- January -> Janvier April...
  • Yair Altman (44 days 19 hours ago): I posted my treeTable utility 10 years ago for anyone to use freely, on an as-is basis, without any warranty, updates or support. If you need any customization or assistance...
  • JY (44 days 20 hours ago): Yair, could you respond to this question please? I’m also finding it difficult to implement such a multi-hierarchy table. Thanks
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top