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 10% 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)

Reviews of the book on Amazon (use the scroll-bar on the right to see all reviews):

Scroll down to see additional reviews =>

### 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 http://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: http://www.mathworks.com/matlabcentral/newsreader/view_thread/314469 (or: http://bit.ly/rMUCrX) | 2011-12-23 |

1 | 1.5.4 | 26 | 3rd from bottom | …and setPropertyName. | addition | ; use if possible (ref)struct | 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 http://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 | http://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 |
| 2012-07-08 |

3 | 3.1.2 | 86 | 1st from bottom | `JButton` … `JCheckBox` | addition | `JToggleButton` – basic control – – Fulluicontrol(‘style’,'togglebutton’) | 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 or uitable)…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 has an empty uitable`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 utility on the Matlab File Exchange.SaveCurrentEditorFiles | 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 |

Hi Yair,

I have a basic question like as we have Java platform where people use to develop tools, again what is the advantage we are getting by developing tools in MATLAB with/without Java concepts.

I guess and aggrie’s there will be benefits by using MATLAB platform instead of JAVA for tool developments(GUI).

Will you suggest me what are the benefits we are getting by developing tools in MATLAB using JAVA concepts.

Thank you for your time,

Regards,

Sai.

@Sai – since you are already familiar with Java, it makes a lot of sense to keep your GUI in Java, but to develop the core algorithms in Matlab. Matlab is much better than Java for rapidly developing computational algorithms. Using Matlab will significantly decrease your development cycle times, and increase your code’s validity/accuracy. After being used to the power and flexibility of Java GUI you will be disappointed at the much more limited pure-Matlab GUI capabilities. But as I said, if you use Java GUI then you can benefit from both worlds.

Hi Yair,

Thank you very much for your time and information.

Regards,

Sainath M.

Hi Yan,

In MATLAB we can create our GUI by using GUIDE tool or by manual scripting(i.e., on figure) with or without JAVA concepts. What is the main difference between these two procedures:

If so please suggest me which one is flexible with more functionalities for developing the GUI tool.

Thank You for your time,

Regards,

Sai.

@Sai – GUIDE is easier to set-up and design, but harder to maintain than creating a pure GUI from scratch. GUIDE figures also have some [usually small] performance overheads compared to pure GUIs. Also, GUIDE makes it a bit more difficult to integrate Java components in the Matlab figure, although this can of course be overcome.

Both of these routes (GUIDE and manual GUI programming) have their uses, and the ultimate decision depends on what you want your GUI to look like and how you wish it to behave (in other words, your exact requirements).

If you would like my consulting help for your specific project, please contact me offline using the email link at the top right of this page (altmany at gmail dot com).

Thank You very much Yair for your message and concern, I will let you know if I need any help.

I purchased!!

I’m not sure this is the right place to say this, but, I began to read the first chapter of the book and saw ‘java.lang.Thread’ and was excited. But it was saddening to see no more explanation about it because that made me wonder if it is possible to write multithreaded m-file using Java interface. If Matlab allows m-file to only use java objects, not allowing java object to use m-files, then it might not be possible. (Note: I have no experience of Java.)

Anyway, a great purchase!!

Thanks for the work.

@ParangBoo – Matlab is indeed single-threaded in its computational engine, but you can launch and use Java threads as long as they don’t access the main Matlab thread. I explain some more about this in section 3.2. If you need to access the Matlab engine and/or data, consider using Matlab timers instead of Java threads.

As to the other part of your question, accessing Matlab from within Java is the topic of the entire chapter 9. If you just need to access the m-file (i.e., the physical source-code text file) rather than Matlab code/data, then you can easily do this from within Java using Java’s extensive I/O capabilities.

@ParangBoo, if you’re interested in multithreading in Matlab and have C++ experience, you can fall back on mex files for this. I recommend the Theron Actors library for simpler C++ multithreading although it is a little hard to get working within Matlab. More recent versions of Theron are even easier to setup and soon you’ll be able to use C++11′s std::thread instead of boost::thread which will simplify things a lot as well.

First of all. Congratulation on your effort and the book. It is very useful.

I read through a few chapters and could not find any examples on the following.

How to instantiate JFrame and use Java Swing without javacomponent? I can do this with non-GUI Java package.

I intent to try using standard Swing, JIDE and other Java package via Matlab command-line without the needs for Matlab figure containers.

Thank you.

You also mentioned that JIDE docs refer to the latest JIDE version, but Matlab uses an older version,(

`com.jidesoft.utils.Lm.getProductVersion()`

) for code-freeze configuration reasons.Is it possible to add JIDE version of my choice to co-exist with Matlab pre-installed version, and force my own function to call/use my specific JIDE version?

In this way, I will have more control over the JIDE versions, just in case the Mathworks decide to change something later.

Thank you.

@Krisada – you can use a different JIDE release with Matlab, but this has been reported to cause problems with Matlab, so use the procedure with extreme care. Naturally, it depends on how far away your selected JIDE release is from Matlab’s.

As for your other question, you can use the

`JFrame`

‘sshow()method to display the frame window. You should really consult a good Java Swing reference if you’re planning to go down this route, because your question tells me that you don’t have much experience in this. Also keep in mind that you will have much difficulty if you ever wish to integrate Matlab controls/axes in such a pure-Java container.Well, I have been deploying codes through Matlab Java Builder for my lab project. My applications consist of deploying a 3D simulation(a robot simulation). Although, Matlab Java Builder JA helps me deploy the code but the animation for any simulation could not be achieved through Java builder. I believe that JMI wrapper can help me achieve in remote accessing MATLAB through client. Will this require to install MATLAB at the client end..?

I would like to buy this book for implementing the above stated purpose. I would be grateful if you could provide me some tutorials for remote accessing Matlab through Java. So, that I can convince by boss to invest in the book.

Thank you.

@Neha – you’re joking right? you need to invest hours of your time and mine in order to convince your manager to buy a relevant book at $50? are you so low-paid that your time is not worth this much? I am certainly not and the few dollars in royalties that I would get from your book copy is not a very large incentive for me to give you hours-worth of free consulting, so sorry.

Hello Yair!!

I am just entering in Matlab “world”…i aim to translate a Java code to Matlab code..i am not a java developer, i am just an user of a Java based application…I think that, starting from ZERO, i could have more possibility to learn to manage a Matlab code, better than a Java one…without needing to ask all time to our java developer to make the modification i need…just make it my own. But the first step, traslate the java source code into matlab…may be the most difficult i suppose. Your book called my attention…do You think it would be useful for my purpose? THANK YOU IN ADVANCE!! Marco

@Marco – you might be better served by reading an introduction-to-Matlab book.

Hi Yair,

sir is there any way to introduced auto-complete feature just like in google search bar. And i had one standalone application created in matlab i want to create license for that is there any way to do same. Please kindly suggest me any way.

@Akash – Auto-complete lists are discussed in page 296 of my book. You can also code such a feature yourself, if you wish.

Re licensing, you may find my article on unique computer ID helpful.

Hi Yair,

Sir is there any way to run rss feeds on matlab gui

@Akash – you can integrate a browser window in your GUI, pointing the displayed webpage to an RSS feed reader.

Is it possible to call Java code from matlab to do some fancy parallel distributed database work?

Matlab database handing is too slow. I also hate that long long sql in Matlab. I want to use JAVA for example Hadoop to do the database work and transfer it back to the Matlab.

I would love to consider buy the book!! Thanks!!

@Hongbo – yes, it is possible to do this and the book shows how. It even has a large section showing how to directly call an external Database from Matlab using JDBC.

Hi Yair,

I’ve read the book but I’m not able to modify the icon of uigetfile.

I use for the “figure”:

How can I modify the uigetfile icon?

Best Regards,

Aim

@Aim – I am not sure that doing this is in accordance with the Matlab end-user license, so I will not be able to help you here.

Hello sir i want to run timer function in matlab :

m create timer in following way but it not properly executed

my main concern is test_Callback uses all variables aur valuse define in main program for which i pass handles but it can’t work

please suggest me some way to do it

@Akash – read here

Sir,

I would like to know that if I create a GUI using JAVA then on the buttons can the MATLAB code be implemented?

Thank you

@Gaurav – If you create your GUI in pure Java then you’ll need to use the Matlab Builder for Java toolbox to connect your GUI to Matlab code.

I’m getting this error. Can anyone please explain why is this happening….?

Error using .^

Not enough input arguments.

Thank you for all of your posts and your book. Great Stuff!!

Hi Yair,

I’ve been interested in learning the ins and outs of integrating java into my gui’s for a while now. This book looks great but is the content still supported by more recent matlab versions?

@Trevor – yes it is, excluding a few quirks here and there. The vast majority of the contents still remains relevant to this day.

Hello Yair,

Any plans to release a new edition of the book “Undocumented Secrets of MATLAB-Java Programming”?

Regards,

Uche

@Uche – there are no plans to update the book in the near future. The vast majority of the book remains valid to this very day.

I’m a student from China,now i want to use Matlab’s function library in java project, ask u a question, can i get a matlab library where i can use all the matlab’s functions outside matlab without using “matlab builder for ja”;by the way ,can i read your book? do u have ebook? i am astudent ,i sorry to tell u that i have not enough money to pay for the really book.

You can use JNI or JMI to access Matlab from Java, but you need to have Matlab installed and running in order to do that – you cannot use the Matlab libraries directly without Matlab running unless you use the Java Builder product (which uses the MCR).

As for an ebook – you can get it from the same place that you get the print book. You can ask your university department library to order a copy of the book.