- Enclose with " for exact match e.g., "uitable report"
- Inter-Matlab data transfer with memcached
- Property value change listeners
- Online (web-based) Matlab
- Class object tab completion & improper field names
- Using pure Java GUI in deployed Matlab apps
- USA visit, 22-31 July 2014
- Java class access pitfalls
- Function call timeline profiling
- Bar plot customizations
- Plotly graphs in IPython Notebook
- Matlab callbacks for Java events in R2014a
- Animated busy (spinning) icon
- Transparent uipanels
- uicontextmenu performance
- Desktop (34)
- Figure window (34)
- Guest bloggers (43)
- GUI (110)
- Handle graphics (52)
- Hidden property (32)
- Icons (7)
- Java (132)
- Listeners (17)
- Memory (12)
- Mex (9)
- Presumed future risk (263)
- Semi-documented feature (7)
- Semi-documented function (31)
- Stock Matlab function (96)
- Toolbox (2)
- UI controls (36)
- Uncategorized (9)
- Undocumented feature (125)
- Undocumented function (28)
TagsActiveX Callbacks COM Compiler Desktop Donn Shull Editor FindJObj GUI GUIDE Handle graphics HG2 Hidden property HTML Icons Internal component Java JavaFrame JIDE JMI keyboard Listener Malcolm Lidierth Matt Whitaker MCOS Memory Menubar Mex Performance Profiler Pure Matlab schema schema.class schema.prop scribe Semi-documented function Toolbar uicontrol UIInspect uitable uitools uiundo Undocumented feature Undocumented function Undocumented property
- Yair Altman (3 hours 7 minutes ago): @MI – I will be very happy to help you integrate sophisticated table controls in your Matlab GUI, for a consulting fee. Please contact me via email (altmany at gmail).
- MI (3 hours 48 minutes ago): Hi Yair, very good work! keep it up! Actually I find it very useful to have a CellEditor for numeric matrix or for mixed-type table-data (cell-array). Therefor I would like to use your...
- Fred (1 day 14 hours ago): I came here since I wanted to run an old version MATLAB on Win8. I wasn’t intending to test with the newer jre. But it could not find the default jre, and it turns out this is a...
- Yair Altman (1 day 15 hours ago): @Ilan – I don’t have access to Linux at the moment so I’m afraid someone else would need to answer this…
- Ilan Bar (2 days 3 hours ago): Yair, mxGetPropertyShared is working great on Windows. Do you know whether there is a way to use it on LINUX as well ? The generic solution solution by James Tursa works consisting...
- Yair Altman (2 days 20 hours ago): @Steven – read here: http://undocumentedmatlab.com/ blog/matlab-callbacks-for-java -events-in-r2014a In short, add the following before calling the set command: jTextArea...
- Steven Baete (3 days 17 hours ago): Great post! It helped me to show the command line output in my own GUI. % to read the matlab output into our GUI jDesktop = com.mathworks.mde.desk.MLDeskt op.getInstance;...
- Luis Vieira da Silva (5 days 3 hours ago): Thanks a lot, Yair, it works fine
- Mark Mikofski (5 days 13 hours ago): Turns out that the newer schooner/whalin client is even faster than the old whalin (meetup.com) client, and they say it’s faster than both spymemcached (from...
- Mark Mikofski (5 days 14 hours ago): In order to get rid of that message and output logging messages to a file configure the Apache.org log4j logger yourself by creating a FileAppender with the default layout and...
- Yair Altman (5 days 15 hours ago): @Luis – simply switch the order of the actions: set(hfig,'Resize','off'); try jFrame.fHG1Client.setMaximized (true); % HG1 catch jFrame.fHG2Client.setMaximized (true); %...
- Luis Vieira da Silva (5 days 19 hours ago): Hi, On figure creation, I would like to maximize it (easily done with your method) and then prevents users to change its size. To do so I use documented Matlab function...
- walderich (6 days 5 hours ago): Thanks a lot for the “EngineInterface” feature. But in my case I had to set its value to 1001 to get it working. I tested this with Matlab versions R2010b-R2014a.
- Bill Doss (11 days 21 hours ago): @Yair … I’ll try that approach. Thanks again. Bill
- Greg (12 days 2 hours ago): Thanks, this works well for my patch plots. Do you know if there is a similar command to smooth the edges of the patch itself such that if I turn off the lines of my patch the block...
Once again I welcome guest blogger Mark Mikofski. Mark has written here last year about JGIT-Matlab integration and earlier this year on JSON-Matlab integration. Today, Mark shows how to use the well-known open-source memcached library for data transfer between separate Matlab processes. Readers are also referred to Mark’s article on his blog. I have already written about various ways to communicate between separate Matlab processes – today’s article is an important complement to these methods.
The memcached open-source library is great for sending objects from process to process. It is typically used to cache large data objects from databases or in a distributed environment, but we can also use it as a simple distributed shared memory system, which stores data in key-value pairs (as in a hashtable).
I was able to use memcached to set and retrieve objects from two different instances of Matlab. Both the Java and .NET variants of memcached can be used for this. memcached requires two components: server and client: The server comes precompiled, while the client needs to be compiled by us.
The memcached server was compiled by Trond Norbye from Couchbase (previously Membase and Northscale) for 32-bit and 64-bit Windows OS. See below how to run it as a service using the Python PyWin32 package.
I compiled Windows binaries of memcached-client libraries for both the Java and .NET using Java-1.7 and Visual Studio 2010 and zipped them up here.
Note: additional (non-memcached) shared-memory implementations used in Matlab include Joshua Dillon’s sharedmatrix (also see here), and Kevin Stone’s SharedMemory utilities, which use POSIX shared-memory and the Boost IPC library. Rice University’s TreadMarks library is another example of a shared-memory approach that has been used with Matlab, in the MATmarks package (note that the current availability of MATmarks is unclear).
For performance reasons, it is almost always better to respond to events (asynchronously), than to continuously check a property value (synchronous polling). Therefore, if we wish to do something when some property value is changed (e.g., log the event, shut down the system, liquidate the portfolio, call the police, …), then it is preferable to attach a property-change listener callback.
The standard (documented) way of attaching a value-change listener to Matlab class properties is via the addlistener function. This only works for handle (not value) classes, and only to those properties that have the SetObservable and/or GetObservable attribute turned on:
addlistener(hClassObject, propertyName, 'PostSet', @myCallbackFcn);
This is all nice and well for Matlab class properties, but what about HG (handle Graphics: plots & GUI) properties? Can we similarly listen to changes in (say) the axes limits? Until now this has been possible, but undocumented. For example, this will trigger myCallbackFcn(hAxes,eventData) whenever the axes limits change (due to zoom, pan, plotting etc.):
addlistener(gca, 'YLim', 'PostSet', @(hAxes,eventData) myCallbackFcn(hAxes,eventData)); % Or (shorter equivalent): addlistener(gca, 'YLim', 'PostSet', @myCallbackFcn);
This could be very useful when such properties could be modified from numerous different locations. Rather than updating all these location to call the relevant callback function directly, we simply attach the callback to the property-change listener. It could also be useful in cases where for some reason we cannot modify the source of the update (e.g., third-party or legacy code).
In addition to
PostSet, we could also set listeners for
PreSet. Also, we could set listeners on
PreGet – this could be useful for calculating dynamic (dependent) property values.
For many years I searched for a good solution for deploying Matlab applications in online web-pages. There used to be a solution over a decade ago but it was taken off the market. We could use the Java Builder to wrap our Matlab apps in a JAR that could be called by a Java-based application server, but we’d still need to develop the front-end web GUI as well as the middle-tier plumbing. A similar limitation exists if we wish to use the new Matlab Production Server (MSP). Developing this front-end and middle-tier is by no means a trivial exercise, as much as MathWorks presentations would like it to appear. Not to mention the fact that the Builder and the MSP are relatively costly (~$5-7K and ~$30K respectively).
I was thrilled to see the answer in the recent Matlab Computational Finance Conference, London. I presented at last year’s conference, so I was excited to review this year’s presentations when I came across a nugget in Kevin Shea’s keynote presentation on new Matlab developments. Note that there were two separate Matlab computational-finance events in 2014 – in London (June 24) and NY (April 9); the interesting piece is from London. Unlike the NY conference, the London conference proceedings do not include a video recording, only the slides (perhaps the video will be added to the proceedings page later, after all it takes some time to process). The last slide of Kevin’s presentation shows a screenshot of a Chrome browser displaying what appears to be a full-fledged Matlab desktop (Workspace, Editor, Command Window, figures) at https://matlab.mathworks.com:
Matlab Online (click to zoom)
I recently consulted to a very large industrial client. Following a merger/acquisition, the relevant R&D department had two development groups using different technologies: one group uses Matlab, the other does not. My client wanted both groups to use Matlab, something that would naturally please MathWorks.
Unfortunately, it turns out that a technical challenge was preventing this move: the other technology enabled data field names (identifiers) longer than Matlab’s namelengthmax=63 characters, and these names also sometimes contained illegal identifier characters, such as spaces or symbols. This prevented an easy code transition, indefinitely delaying the migration to Matlab.
Accessing improper fieldnames
I suggested to this client to use Matlab class objects that overloaded the subsref() and subsasgn() methods: the long original identifiers would be stored in some internal container (cell array or containers.Map etc.), and they would be accessed not directly (which would be impossible since they are not valid Matlab identifiers) but via the overloaded methods. Something along the following lines:
I would like to welcome repeat guest blogger Karthik Ponudurai, who has previously written here about the JTattoo look-and-feel, and about integration of Java controls’ events with Matlab callbacks. Today, Karthik discusses a problem when we wish to use pure-Java GUIs in deployed Matlab programs, and the undocumented simple solution.
Reminder: I will be visiting several US cities (Minneapolis, Boston and New York) in July 22-31 (details). Let me know if you’d like to meet me there.
Using a pure-Java Swing-based Graphical User Interface (GUI) has several important advantages compared to using a pure-Matlab GUI:
- Java GUI widget toolkit provides a large collection of components (scrollbar, slider, etc…) and layouts (card, spring, etc…)
- Multiple event handling options
- Mature third-party Java Swing IDEs for quick and easy GUI development (Netbeans, Eclipse, etc…)
- Supports pluggable look and feel that allows applications to have a look and feel that is unrelated to the underlying platform.
- Java Swing’s window icon can be modified, whereas Mathworks apparently places an [entirely unreasonable] license restriction on modifying the Matlab figure window icon.
When Matlab is in development (non-deployed) mode, we can load and display a Java
JFrame using the following Matlab code:
% Add Java library to dynamic Java classpath javaaddpath([pwd '\ExampleWindow.jar']); % Get example Java window from the library jFrame = examplewindow.JavaWindow(); % Get Java buttons % Note: see http://UndocumentedMatlab.com/blog/matlab-callbacks-for-java-events-in-r2014a plotMeshButton = handle(jFrame.getPlotMeshButton(), 'CallbackProperties'); showWarnButton = handle(jFrame.getShowWarnDlgButton(), 'CallbackProperties'); % Set Java button callbacks set(plotMeshButton, 'ActionPerformedCallback', @myPlotMeshCallback); set(showWarnButton, 'ActionPerformedCallback', @myShowWarnCallback); % Display the Java window jFrame.setVisible(true);
Java JFrame created in Matlab (click for full-size image)
All this works great when ran within the Matlab environment. However, when the Matlab source code file (.m) is compiled as a Windows standalone executable application, when running the resulting executable the Java window appears for a short period and then terminates (exits). Without a visible Matlab figure, the compiled Matlab application does not retain the Java window open. The JFrame flashes on for a split-second, and then vanishes.
- July 22-24: Minneapolis, MN
- July 25-27: New York City, NY
- July 28-31: Boston, MA
- July 31: New York City, NY
I will be happy to meet you at any of these locations to discuss how I could bring value to your Matlab programming needs, as a consultant, contractor or trainer.
If you would like to schedule a face-to-face meeting, please email me.
A few days ago I consulted to a company that wanted to integrate some of their Java code in Matlab. The Java code compiled ok and ran just fine in the Java environment, but when we wanted to use the relevant Java classes in Matlab we got all sorts of errors. Since I believe that this could be a common problem, and I’m not sure that there’s any other place that does this, I thought to list the possible error causes in today’s article:
- Forgetting or mistyping the package name:
>> jObject = JButton('Click me!'); % missing package name Undefined function 'JButton' for input arguments of type 'char'. >> jObject = java.swing.JButton('Click me!'); % should be javax.swing... Undefined variable "java" or class "java.swing.JButton". >> jObject = javax.Swing.JButton('Click me!'); % should be javax.swing... Undefined variable "java" or class "javax.Swing.JButton". >> import java.swing.* % should be javax.swing... - no error here, only in the next line >> jObject = JButton('Click me!'); % missing package name Undefined function 'JButton' for input arguments of type 'char'. >> import java.swing.JButton % should be javax.swing... Error using import Import argument 'java.swing.JButton' cannot be found or cannot be imported.
Note: package names are typically lowercase, but 3rd-party packages might not follow this convention! Since Java is case-sensitive, the package name must be exact.
Back in 2012, I wrote an article about the Matlab profiler’s undocumented functionality of storing detailed information about the function-call history, including timing information. I intended to follow up on this with a utility that would bring the data to life, but I never found the time for this. With my upcoming book on Matlab performance now nearing publication (ETA: November 2014), I decided it’s finally time to finish the task. The result is my profile_history utility, available for download on the Matlab File Exchange (open-source):
Usage of profile_history is very easy, based on the latest profiling session:
profile on; surf(peaks); profile_history
Function call timeline profiling (click for full-size image)
Over the past few years I have shown numerous uses of Java components in Matlab, many of them built-into the Matlab environment and can be used as-is. I have been asked many times how I uncover these components and how I discover how to use them. The short answer is that this is indeed often not easy. The blog articles (and my book) only present “success stories“, i.e. cases where I was able to uncover the behavior, functionality and usage of such components; they do not present the numerous other cases where I have not been so successful.
Still, over the course of the past 10-15 years, I developed several tools that help me become more efficient in this work, and I’ve posted most of these on the File Exchange. I’ve already written here about the FindJObj utility, both as a means for uncovering the underlying Java reference of Matlab GUI controls, and as a means to visualize the GUI hierarchy of a specified window or container. I also wrote about my uiinspect utility, which analyzes the specified object in a compact and intuitive GUI window.
Today I wish to complete the picture by describing my checkClass utility. checkClass is more limited than uiinspect in the types of objects that it can analyze (almost only Java objects) and the information that it presents (for example, callbacks and properties are not listed separately). checkClass‘s main benefit is the fact that it is a very lightweight (230 lines of code), fast, GUI-less console application. checkClass highlights the differences between the analyzed class and its superclass, which is especially useful when investigating the functionality of objects. During the course of my work, I use checkClass for 95% of my investigations, and uiinspect/findjobj only for the remainder. For Java objects at least, a detailed console listing of the methods is quite enough, since properties are typically accessed via getPropName()/setPropName() accessor methods. The combination of checkClass and uiinspect can be very useful when investigating a new Java library or integrating a 3rd-party API.
Here is a sample output for the
>> jButton = com.mathworks.mwswing.MJToggleButton; >> checkClass(jButton) % or: jButton.checkClass Class com.mathworks.mwswing.MJToggleButton (uiinspect) Superclass: javax.swing.JToggleButton Methods in MJToggleButton missing in JToggleButton: hasFlyOverAppearance() : boolean hideText() isAutoMnemonicEnabled() : boolean isTextHidden() : boolean setAutoMnemonicEnabled(boolean) setFlyOverAppearance(boolean) setFocusTraversable(boolean) unHideText() Methods inherited & modified by MJToggleButton: getForeground() : java.awt.Color getParent() : java.awt.Container isFocusTraversable() : boolean paint(java.awt.Graphics) setAction(javax.swing.Action) setModel(javax.swing.ButtonModel) setText(java.lang.String) Interfaces in JToggleButton missing in MJToggleButton: javax.accessibility.Accessible Interfaces in MJToggleButton missing in JToggleButton: com.mathworks.mwswing.ExtendedButton
Bar charts are a great way to visualize data. Matlab includes the bar function that enables displaying 2D bars in several different manners, stacked or grouped (there’s also bar3 for 3D bar-charts, and barh, bar3h for the corresponding horizontal bar charts).
Displaying stacked 1D data
bar is basically a high-level m-file wrapper for the low-level specgraph.barseries object. Such wrappers are generally a good thing, assigning default property values, checking the input data for illegal values etc. But in some cases, they are simply too smart. One such case happened to me a few days ago, when I needed to display a stacked chart of some data values. This works great when the data is a 2D matrix, but when the data happens to be 1D (a single row), bar decides to display the values in separate bars (left screenshot), rather than in a single stacked bar (right screenshot), even when I specifically ask for a ‘stacked’ graph: