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.
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
Reviews of the book on ComputingReviews
Reviews of the book on matlab.developpez.com (French)
Table of Contents
PREFACE
CHAPTER 1: INTRODUCTION TO JAVA IN MATLAB CHAPTER 2: USING NON-GUI JAVA LIBRARIES IN MATLAB CHAPTER 3: RICH GUI USING JAVA SWING CHAPTER 4: UITOOLS CHAPTER 5: BUILT-IN MATLAB WIDGETS AND JAVA 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 CHAPTER 8: THE MATLAB DESKTOP CHAPTER 9: USING MATLAB FROM WITHIN JAVA CHAPTER 10: PUTTING IT ALL TOGETHER 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
- 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.
- 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.
- 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.
- 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.
- 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, 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/articles/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... |
fix | jButton = javahandle_withcallbacks... |
2012-07-08 |
3 | 3.1.2 | 86 | 1st from bottom | JButton … JCheckBox |
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 |
fix | jscroll.java.VERTICAL_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 |