MATLAB-Java Programming book

Undocumented Secrets of Matlab-Java Programming book
CRC discount promo code

Quick links:     Reviews     Table of Contents     Book organization     FAQ     About the author     Errata list

For a variety of reasons, the Matlab-Java interface was never fully documented. This is really quite unfortunate: Java is one of the most widely used programming languages, having many times the number of programmers and programming resources as Matlab. Also unfortunate is the popular claim that while Matlab is a fine programming platform for prototyping, it is not suitable for real-world, modern-looking applications.

Undocumented Secrets of Matlab-Java Programming (CRC Press, ISBN 9781439869031) aims to correct this.

This book shows how using Java can significantly improve Matlab program appearance and functionality. This can be done easily and even without any prior Java knowledge.

Readers are led step-by-step from simple to complex customizations. Within the book’s 700 pages, thousands of code snippets, hundreds of screenshots and ~1500 online references are provided to enable the utilization of this book as both a sequential tutorial and as a random-access reference suited for immediate use.

Click here to get your book copy now!
Use promo code MZK07 for a 25% discount and free worldwide shipping on crcpress.com

This book demonstrates how:

  • The Matlab programming environment relies on Java for numerous tasks, including networking, data-processing algorithms and graphical user-interface (GUI)
  • We can use Matlab for easy access to external Java functionality, either third-party or user-created
  • Using Java, we can extensively customize the Matlab environment and application GUI, enabling the creation of visually appealing and usable applications

No prior Java knowledge is required. All code snippets and examples are self-contained and can generally be used as-is. Advanced Java concepts are sometimes used, but understanding them is not required to run the code. Java-savvy readers will find it easy to tailor code samples for their particular needs; for Java newcomers, an introduction to Java and numerous online references are provided.

No toolbox, Simulink or Stateflow is necessary for using this book – only the core Matlab product. These extra tools indeed contain many other Java-based aspects, but they are not covered in this book. Perhaps a future book will describe them.

This book shows readers how to use and discover the described components, using nothing but Matlab itself as the discovery tool. In no case is illegal hacking implied or necessary for the discovery or usage of anything presented in this book. As far as I know, everything in this book is legal and within the bounds of the Matlab license agreement.

Reviews

Amazon reviews - Undocumented Secrets of Matlab-Java ProgrammingReviews of the book on ComputingReviews

Reviews of the book on matlab.developpez.com (French)

Reviews of the book on Amazon

Table of Contents

PREFACE

CHAPTER 1: INTRODUCTION TO JAVA IN MATLAB
   1.1 Creating Java Objects
   1.2 Java Object Properties
   1.3 Java Object Methods and Actions
   1.4 Java Events and Matlab Callbacks
   1.5 Safe Java Programming in Matlab – A How-to Guide
   1.6 Compiling and Debugging User-Created Java Classes in Matlab
   1.7 Compatibility Issues
   1.8 Java Versions in Matlab
   1.9 Java.opts

CHAPTER 2: USING NON-GUI JAVA LIBRARIES IN MATLAB
   2.1 Complex Data Structures
   2.2 Database Connectivity
   2.3 Miscellaneous Other Uses
   2.4 A Short Pause for Reflection

CHAPTER 3: RICH GUI USING JAVA SWING
   3.1 Adding Java Swing Components to Matlab Figures
   3.2 Matlab’s Main Thread and the Event Dispatch Thread (EDT)
   3.3 Customizing Java Components
   3.4 Component Callbacks
   3.5 Using 3rd-Party Libraries in Matlab
   3.6 System-Tray Icons
   3.7 Drag and Drop
   3.8 Adding Matlab Components to Java Swing Containers
   3.9 Alternatives to Swing

CHAPTER 4: UITOOLS
   4.1 Uitable
   4.2 Uitree
   4.3 Uitab
   4.4 Uiundo
   4.5 Toolbars
   4.6 Menus
   4.7 Status Bar

CHAPTER 5: BUILT-IN MATLAB WIDGETS AND JAVA CLASSES
   5.1 Internal Matlab Java packages
   5.2 MWSwing package
   5.3 MWT package
   5.4 MLWidgets package
   5.5 Widgets package
   5.6 MLServices package
   5.7 JIDE
   5.8 Miscellaneous other internal classes

CHAPTER 6: CUSTOMIZING MATLAB CONTROLS
   6.1 PushButton
   6.2 ToggleButton
   6.3 RadioButton
   6.4 Checkbox
   6.5 Editbox
   6.6 Listbox
   6.7 Popup menu (a.k.a. drop-down, combo-box)
   6.8 Slider
   6.9 Text label
   6.10 Frame
   6.11 Uipanel
   6.12 Tooltips

CHAPTER 7: THE JAVA FRAME
   7.1 Java Frame Properties and Methods
   7.2 FindJObj and the Java Frame Components Hierarchy
   7.3 Important Java Frame Containers
   7.4 BeanAdapters

CHAPTER 8: THE MATLAB DESKTOP
   8.1 Desktop Functionality and Layout
   8.2 System Preferences
   8.3 Command Window
   8.4 Editor
   8.5 Keyboard Bindings
   8.6 Workspace
   8.7 Other Desktop Tools

CHAPTER 9: USING MATLAB FROM WITHIN JAVA
   9.1 Approaches for Java control of Matlab
   9.2 JMI – Java-to-Matlab Interface
   9.3 JMI wrapper – local MatlabControl
   9.4 JMI wrapper – remote MatlabControl
   9.5 Using JNI to connect Java and Matlab

CHAPTER 10: PUTTING IT ALL TOGETHER
   10.1 UISplitPane
   10.2 IDS – Integration Debriefing System
   10.3 Concluding Exercise: UIMultiListbox

APPENDIX A: WHAT IS JAVA?

APPENDIX B: UDD

APPENDIX C: OPEN QUESTIONS

INDEX

Book organization

This book is organized in chapters grouped by related functionality/usage and ordered from easiest (novice Java use) to advanced. It is NOT necessary to read the book in order: the chapters and sections are mostly independent and can stand alone. You can safely skip almost any section that you find difficult or uninteresting.

Chapter 1 (Introduction to Java in Matlab) provides a description of the internal Java engine shipped in Matlab. Chapters 2 (Using Non-GUI Java Libraries in Matlab) and 3 (Rich GUI using Matlab Swing) describe how this internal Java engine can be used to extend Matlab, both programmatically (Chapter 2) and visually (Chapter 3).

Chapter 4 (Uitools) describes a set of undocumented built-in Matlab user-interface functions which use Java components. Many of these functions are Matlab wrappers for Java components presented in Chapter 3. Using these tools and some simple customizations we can significantly improve our Matlab programs’ usability.

The notion of customizing our Matlab user-interface using Java is expanded in Chapters 5 (Built-in Matlab Widget and Java Classes), 6 (Customizing Matlab Controls) and 7 (The Java Frame).

Chapter 8 (The Matlab Desktop) discusses customization of the Matlab environment rather than that of a Matlab application.

Chapter 9 (Using Matlab from within Java) discusses the other side of the coin, namely how to call the Matlab engine from within a Java program. Unlike calling Java from Matlab, and unlike calling Matlab from C/C++/VB, the Java-to-Matlab interface is entirely undocumented and unsupported – Chapter 9 fills this gap.

This book concludes with Chapter 10 (Putting It All Together), which describes a utility and an application that tie together many issues presented in this book.

From a supportability viewpoint, progressively advanced chapters of this book are deeper in undocumented territory, are less supported, have fewer online references and are increasingly prone to change or malfunction in some future Matlab release.

A quick FAQ

  1. I do not know Java – is this book for me?
    Absolutely yes. This book is intended for Matlab programmers and users, and no Java knowledge is assumed. Java-savvy programmers will indeed find it easier to use and extend some of the more advanced topics. However, even programmers with absolutely no Java experience can still use most of this book as-is. I hope the presentation will suit both audiences equally well.
     
  2. Is everything legal?
    Yes. I am an engineer, not a lawyer, but as far as I can tell everything presented in this book is perfectly legal to use, as long as you have access to a legal version of Matlab. Still, if you have any specific concern about a particular aspect, I am sure that MathWorks will gladly answer your question.
     
  3. Does MathWorks endorse this book?
    Unfortunately not. This book often relies on undocumented and unsupported features. MathWorks allows us to use these features, but does not officially endorse or support them.
     
  4. How is this book different from the articles on this website?
    Almost all of the Java-related articles on this website are included in the book, but they constitute only a part of the book. The book’s 700 pages contain much more information than the website articles. The book is also structured in a way that enables quick discovery of the features relevant to your needs, rather than the salad of articles in this website. On the other hand, this website is continually updated with new information in articles and reader comments, and is therefore a valuable resource in its own right. You can also easily copy-paste code from the website articles, which is somewhat more difficult to do with a printed book… For these reasons, I included back-links to the relevant articles within the corresponding book sections.
     
  5. How can I help to promote this work?
    You can help by sending feedback and by promoting this book and the website to colleagues. You could write a review about the book in websites such as Amazon or Barnes & Noble. You may also support my work by hiring my services for consulting or programming work.
     

About the author

Yair Altman Yair Altman, author of the extremely popular UndocumentedMatlab.com website, is well respected in the Matlab community as an expert on undocumented Matlab features and the Matlab-Java interface in particular. His many years of contribution on the Matlab (CSSM) forum and his website, including a multitude of useful tips never before published, are now available in this highly readable book.

Yair holds a BSc in Physics and an MSc in Computer Science, has over 20 years of programming experience at various levels of responsibility, and currently consults for
several software development projects.

Additional information can be found here.

Yair is married with three children (who would never have allowed him to write this book had they known in advance the herculean effort that it would take…).

Errata list

Last updated: 2012-07-08

Chapter Section Page Paragraph Current text Correction type Corrected text Post date
1 1.1.1 6 2nd from bottom (none – new paragraph) addition Important: do not exit your Java GUI using Java’s exit() function, because this exits Matlab. This is not a bug: since the Matlab GUI is itself Java-based, exiting Java (or more precisely, the JVM), has a side effect of closing Matlab. 2011-12-23
1 1.1.2 10 5th from bottom (none) clarification See https://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/ for details about converting Java arrays into Matlab arrays. 2011-12-23
1 1.2 13 2nd from bottom …equivalent Java form: clarification In some cases there is no corresponding Java setter method – in such cases we need to use other workarounds: https://www.mathworks.com/matlabcentral/newsreader/view_thread/314469 2011-12-23
1 1.5.4 26 3rd from bottom …and setPropertyName. addition ; use struct if possible (ref) 2012-02-19
1 1.6 33 3rd from bottom (none – new paragraph) addition I have never tested this myself, but theoretically one could replace any JVM or MathWorks Java class with a user-modified version, using the –Xbootclasspath JVM startup option (see http://weblogs.java.net/blog/alexfromsun/archive/2006/06/swing_testing_a.html (or: http://bit.ly/uNJYar)). This could help debugging by, for example, adding debug printouts. 2011-12-23
1 1.8.2 41 3rd from top …Apple Java 10.6 update 1 addition … or by a java.opts fix (see https://www.mathworks.com/matlabcentral/newsreader/view_thread/314519#859363 (or: http://bit.ly/ty5B7S)). 2011-12-23
1 References 53 Refs #141, 143 (none) addition https://www.mathworks.com/matlabcentral/newsreader/view_thread/314519#859363 (or: http://bit.ly/ty5B7S) 2011-12-23
2 2.2.3 65 bottom DriverManager.getConnection( fix con = DriverManager.getConnection( 2012-02-05
2 2.2.4 66 3rd from bottom executeQuery() fix executeUpdate() for the DML (update) queries – (see the following correction in this table) 2012-02-19
2 2.2.4 66 3rd from bottom (none – new paragraph) addition SQL queries that return a ResultSet should use executeQuery(); queries that update data should use executeUpdate(), which returns the number of affected rows (ref). Some JDBC-database combinations accept executeQuery() in both cases; but in many cases this is prevented and attempting to update data using executeQuery() will result in a run-time exception informing this. If you are unsure which of the execute methods to use, you can use execute(). This method runs both kinds of SQL statements, and returns a boolean value based on the inferred statement type: true=executeQuery(); false=executeUpdate() (ref). 2012-02-19
2 2.4 73 3rd from top …and source-code repositories,(107) fix (the reference number should be 106, not 107) 2012-02-05
2 References 74 Ref #13 …/containers_map.html fix http://www.mathworks.com/help/techdoc/ref/containers_mapclass.html (or: http://bit.ly/wtY6Wz) 2012-02-19
2 References 75 Ref #44 (none) addition http://www.mathworks.com/matlabcentral/answers/17127-connect-to-postgres-db (or: http://bit.ly/pDnfzF) 2011-12-23
3 3.1.1 82 3rd from top hButton = javahandle_withcallbacks...
jButton = 158.002197265625
fix jButton = javahandle_withcallbacks...
hButton = 158.002197265625
2012-07-08
3 3.1.2 86 1st from bottom JButtonJCheckBox addition JToggleButton – basic control – uicontrol(‘style’,’togglebutton’) – Full 2011-12-23
3 3.2 92 2nd from top …with the EDT, clarification EDT deadlocks can at least be monitored and displayed using a dedicated timer thread. See for example http://elliotth.blogspot.com/2005/05/automatically-detecting-awt-event.html (or: http://bit.ly/ug6Ryd). 2011-12-23
3 References 152 Ref #151 (Reference 151) fix (Reference 151 is listed twice. The second reference number should be 152, and all subsequent reference should have a value of +1, e.g. reference 152 is actually 153 etc.) 2011-12-23
4 Introduction 154 2nd from bottom Some functions (such as uitable or uitree)… fix Some functions (such as uitree) 2011-12-23
4 4.1.1 162 4th from bottom …with a simple DefaultTableModel: clarification This in itself is not enough – we also need to ensure that the uitable has an empty ColumnFormat property, because if it is non-empty then it overrides our cell-renderer. 2011-12-23
4 4.1.3 168 3rd from bottom jscroll.VERTICAL_SCROLLBAR_AS_NEEDED
jscroll.HORIZONTAL_SCROLLBAR_AS_NEEDED
fix jscroll.java.VERTICAL_SCROLLBAR_AS_NEEDED
jscroll.java.HORIZONTAL_SCROLLBAR_AS_NEEDED
2012-09-22
4 4.1.3 169 3rd from bottom setNonContiguousCellSelection(flag) fix setNonContiguousCellSelection(false) 2012-07-08
4 References 236 Ref #1 http://www.mathworks.com/matlabcentral/fileexchange/10045 fix (Unfortunately, this submission was removed from the File Exchange) 2011-12-23
5 5.2.1 251 4th from top Spinners use an inner model clarification Spinners use an internal data model 2011-02-05
5 5.7 319 4th from top …not the newest 3.2.2 as of September 2011 fix …not the newest 3.3.2 as of December 2011 2011-12-23
5 5.7.1 321 3rd from top (none) clarification There is unfortunately no corresponding FileChooser JIDE class. This is unfortunate due to the internal JVM bug (http://bit.ly/txbDyw) that causes Swing’s JFileChooser to not show the top icons in Windows 7 on Matlab versions up to R2011b (the fix requires a JVM upgrade to 1.6_18, as described in Section 1.8.2 above). JIDE’s FolderChooser works in Windows 7 since it uses internal proprietary icons. 2011-12-23
7 References 462 Ref #10 (none) addition http://weblogs.java.net/blog/alexfromsun/archive/2006/06/swing_testing_a.html (or: http://bit.ly/uNJYar) 2011-12-23
7 References 462 Ref #23 http://www.mathworks.com/matlabcentral/fileexchange/11684 fix (Unfortunately, this submission was removed from the File Exchange) 2011-12-23
8 8.3.4.2 504 top …Until this bug is fixed, I suggest using only simple values. fix I suggest using only simple values in R2010a-R2011b. This bug was apparently fixed in R2012a. 2012-02-05
8 8.4.1 510 5th from bottom (none) addition This functionality can be used to preserve separate editor states for different projects, and has been incorporated in the SaveCurrentEditorFiles utility on the Matlab File Exchange. 2012-02-05
9 9.5 565 2nd from bottom (none) addition Place this class file on Matlab’s static Java classpath (classpath.txt file), to prevent UnsatisfiedLinkError exceptions (see ref). 2012-02-05