Undocumented Secrets of MATLAB
5.1 Internal MATLAB Java Packages
MATLAB has several Java packages (com.mathworks.*) which contain Swing-extended com-
ponents and resources (icon images), which are used internally by the MATLAB application and
can also be used in user applications. All these components reside in packages that have the com.
mathworks domain prex. The packages are grouped based on utility and topic and are all located
in the %matlabroot%/java/jar/ folder in separate JAR (Java archive zip) les.
These Java archive
les are all included in the static Java classpath, within the classpath.txt le. This means that all
internal MATLAB classes are immediately accessible from within our MATLAB m-code.
Important warning: All classes included in MATLAB packages, their internal function-
alities, and even the packages themselves and the location of their containing JAR les,
are entirely unsupported and are prone to change without any warning between MATLAB
releases. Extensive use of exception handling must, therefore, be done in any code which uses
these classes/packages. The description in this chapter focuses on release R2008a (7.6); other
releases may well vary.
Note that in all the ensuing discussions, MATLAB’s internal classes are not hacked or
decompiled — an act which could violate MATLAB’s license agreement. Instead, we shall
only use commands available from within MATLAB or snippets copied from openly accessible
MATLAB m-code les.
When asked about these packages, Mike Katz said the following on the ofcial MATLAB
Desktop blog (http://blogs.mathworks.com/desktop):
“com.mathworks.* represent Java classes that we’ve built here. These mostly represent the
libraries and widgets used by the Desktop and toolboxes, and as such are not considered part
of the MATLAB language and are largely [un]documented. Occasionally we mention some of
these in technical support solutions as workarounds to bug xes. Some of the classes are docu-
mented for use for xml, web, and database I/O, and a whole bunch are meant for use in with the
MATLAB Builder JA product for deploying MATLAB code to work with Java programs.”
5.1.1 Inspecting Package Contents
Since jar les are simple zip les, they can be inspected using any zip client application, includ-
ing MATLAB’s standard unzip command.
zipFilename = fullfile(matlabroot,'/java/jar/mwswing.jar');
classFilenames = unzip(zipFilename, outputFolder);
This will spill all of mwswing.jar’s les (which are the requested classes and some static
resources such as icons) into a tree structure in outputFolder (i.e., outputFolder/com/math-
works/mwswing/. . .) and return a cell-array of the le paths.
For example, C:\Program Files\MATLAB\R2008b\java\jar\mwswing.jar.
Matlab’s built-in unzip.m itself uses standard Java zip-processing classes and methods.
K13163_Book.indb 242 11/8/2011 8:07:59 PM
Widgets and Java Classes
However, although it may help our understanding, we do not really need the physical class
les. We only need to know which les are available in the mwswing package. So, let us use
only a small segment of unzip.m:
>> zipJavaFile = java.io.File(zipFilename);
>> zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
>> files = zipFile.getEntries
>> while files.hasMoreElements, disp(files.nextElement.getName); end
Remember to close the zipFile after reading is done, in order to free resources (this is done
by unzip.m in its cleanup phase):
>> zipFile.close(); % close file even if exception happens!
Note that the reverse operation, namely nding the source JAR of a specic Java object, can
be obtained using standard Java reection. For example, let us nd the JAR source of the Java
object that underlies a simple pushbutton uicontrol:
>> hButton = uicontrol('String','Yair');
>> jButton = java(findjobj(hButton))
com.mathworks.hg.peer.PushButtonPeer$1[. . .]
(file:/C:/Program%20Files/MATLAB/R2010b/java/jar/hg.jar . . .)
5.1.2 Inspecting an Internal MATLAB Class
We are now faced with two important questions: Which of the internal MATLAB packages and
classes are useful, and how can we use them?
The rst question can be answered to a small degree by simple inspection of the le names.
It is pretty obvious that com.mathworks.mwswing.MJToggleButton implements a
K13163_Book.indb 243 11/8/2011 8:07:59 PM