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 (email)
  •  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
  • JCFL (1 day 6 hours ago): I was trying the above trick to change the transparency of contour lines and wondering why not all the lines have changed — then I found your post. Thanks and your post saved me.
  • Patrick Fitzgerald (3 days 9 hours ago): Just a heads up to anyone else digging around for help related to this: if you assign BackrgoundColor to a pushbutton-style uicontrol, it seems to prevent you from...
  • Vasiliy (19 days 5 hours ago): Hi Yair, i’m trying to use the MonthChooserPanel class. com.mathworks.mwswing.MJUtilit ies.initJIDE; handles.jPanel = com.jidesoft.combobox.MonthCho oserPanel;...
  • DM (19 days 5 hours ago): Hi Yair, I’m trying to use the MonthChooserPanel class. com.mathworks.mwswing.MJUtilit ies.initJIDE; handles.jPanel = com.jidesoft.combobox.MonthCho oserPanel; [handles.hPanel,...
  • Yair Altman (22 days 21 hours ago): @Alex – thanks, but keep in mind that new functions will only work on the recent Matlab releases. If your code needs to work on older Matlab releases, you could revert to...
  • Alex Churchill (22 days 22 hours ago): I was looking up how to do this exact task today. I was about to hold my nose and use the internal controllib function, when I happened to chance across a slightly newer...
  • Sebastian Hölz (29 days 4 hours ago): I have not investigated this in detail, but I think one way to go in new versions of Matlab (>2019b) might be to use listeners to suitable figure properties, e.g. fig =...
  • Prathep (29 days 7 hours ago): Hi Yair, Thanks for your introduction on Matlab Toostrip. Is there any way to add Matlab toolstrip for uifigures as you have done already for figure?
  • Josh (35 days 13 hours ago): Dear Yair, Small typo; you wrote >>Move lines up or down – CTRL + ALT + UP or DOWN allows you to move selected lines up or down but it’s actually ALT+SHIFT then UP/DOWN...
  • Yair Altman (42 days 8 hours ago): You can try to increase the Java heap memory size in the Matlab preferences (General => Java Heap Memory). Any changes to the settings will only take effect after restarting...
  • Thomas (42 days 9 hours ago): Hello, thanks for sharing! I currently receive the following memory error message when using savezip with a big object (Matlab R2020b, Windows 10). Error using...
  • Yair Altman (45 days 17 hours ago): Yerlan – either use menuItem1.setEnabled(0) or set(menuItem1,'Enabled',0)
  • Manzn (45 days 19 hours ago): Thank you man! you saved me, when there was no more light 😀
  • Yerlan (47 days 0 hours ago): Hello Mr. Altman, how can I disable menu items in the context menu? E.g. when I am trying to do this: menuItems = jmenu.getSubElements; menuItem1 = menuItems(1);...
  • Yair Altman (47 days 16 hours ago): Thanks for the note Eric – you forgot the crucial call to jTable.setLabelTable(labelTabl e) – I added it into your code snippet above.
Contact us
Undocumented Matlab © 2009 - Yair Altman
Scroll to top