- Enclose with " for exact match e.g., "uitable report"
- Customizing listbox/combobox items
- savezip utility
- 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
- Desktop (34)
- Figure window (34)
- Guest bloggers (43)
- GUI (112)
- Handle graphics (52)
- Hidden property (32)
- Icons (7)
- Java (135)
- Listeners (17)
- Memory (12)
- Mex (9)
- Presumed future risk (266)
- Semi-documented feature (7)
- Semi-documented function (31)
- Stock Matlab function (96)
- Toolbox (2)
- UI controls (38)
- Uncategorized (9)
- Undocumented feature (126)
- Undocumented function (29)
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 (1 day 12 hours ago): Thanks for the comment Sebastian. It is a logical extension of the ActiveX idea, but you are of course correct that it should be made explicit, since using a uitable is a better...
- sebastian (1 day 13 hours ago): FWIW: Another possibility is to use an uitable, with its first column displaying the checkboxes (with logical column format) and the second column showing the non-editable list...
- Hoi Wong (2 days 2 hours ago): Thanks. I’ll try to figure out how to retroactively put the plotted figures into an uipanel. I’m not too used to it other than laying out a GUI. That’s why I...
- Yair Altman (2 days 2 hours ago): Alternatively, use ScreenCapture’s ability to specify a pixel position vector (second input arg) and use the figure’s size to determine the position vector’s...
- Yair Altman (2 days 2 hours ago): Of course it’s possible, it’s just that ScreenCapture doesn’t currently have this feature and I have no plans to add it in the near future. You’ll need...
- Hoi Wong (2 days 3 hours ago): Thanks for the reply. I have simple figures done by subplot() and plot() commands, so just pointing at one axes handle won’t work. Can you point me to a easy way to...
- Yair Altman (2 days 3 hours ago): Best thing is to have a top-level panel. If you don’t have it, try pointing ScreenCapture at the axes handle, but it may perhaps not be accurate because of the...
- Hoi Wong (2 days 3 hours ago): Thanks. Just did that. For simple figures done by plot(), do they have a top level panel handle? All the child objects are of the type ‘axes’.
- Yair Altman (2 days 4 hours ago): @Hoi – thanks. Please remember to add your feedback/vote to the FEX page. You can run ScreenCapture on the figure’s top-level panel handle, assuming you have such a...
- Hoi Wong (2 days 4 hours ago): With ScreenCapture utility, is there a way to capture a figure without the borders and toolbar, title, etc? This tool is awesome and super useful since TMW never managed to do...
- cw (2 days 5 hours ago): This is pretty much one of the most useful posts I’ve encountered in a very long time. Thank you !
- Sergey Kucheryavskiy (2 days 15 hours ago): Yes, now I see it as well: — problems with Children in TabPanel and some others. Actually it was the TabPanel I decided to use the GUI layout, otherwise I feel quite...
- Yair Altman (2 days 15 hours ago): @Dennis – I’m glad I helped. I’m a little disappointed that MathWorks support asks customers to upgrade to a new release (possibly costing extra money and...
- Yair Altman (2 days 15 hours ago): @Sergey – the GUI layout toolbox is incompatible with HG2 in several places, not just the one that you found. MathWorks is aware of this and I expect that they will update...
- Dennis (2 days 15 hours ago): Dear Yair, Thank you for solving my problem: I was experiencing very low frequency line updates on a windows 7 system running R2012b (32bit) and also on R2014a (64bit). I had a...
Last week I wrote about using a variety of techniques to customize listbox items with an attached checkbox icon. Some of these methods used a standard Matlab listbox uicontrol, others used other controls. Today I wish to extend the discussion and show how Matlab listbox and combobox (pop-up) items can be customized in a variety of ways.
To add icons to listbox/combobox items, we could use standard HTML, as I’ve shown last week. This is the simplest method, requires no Java knowledge, and it usually works well. The problem is that when a listbox/combobox has numerous items (hundreds or more), it may start getting sluggish. In such case it is faster to use a dedicated Java cell-renderer that sets the icon, font, colors, tooltip and other aspects on an item-by-item basis. This runs faster and enables far greater customizability than what is possible with HTML. The drawback is that it requires some Java programming. No free lunch…
Listbox and combobox cell-renderers need to extend
javax.swing.ListCellRenderer, similarly to uitable cell-renderers. This is basically a simple Java class that minimally contains just an empty constructor and a getListCellRendererComponent() method with a predefined signature. getListCellRendererComponent() is automatically called by the Swing render engine separately for each listbox item, and gets as input args a
JList reference, the item value (typically a string), an integer list index, a boolean flag indicating whether the item is currently selected, and another flag indicating whether the item is currently in focus. getListCellRendererComponent() uses these parameters to customize and return a
java.awt.Component, which is typically (but not necessarily) a standard Swing
Here is a simple example that displays a folder of icon files in a Matlab listbox and combobox. Each item is the filename, with a customization that if the file is an icon, then this icon is displayed next to the file name, otherwise the name appears in red italic without an icon. For illustration, we’ll use Matlab’s builtin icons folder: %matlabroot%/toolbox/matlab/icons/:
Several years ago I blogged about using a checkbox-tree in Matlab. A few days ago there was a question on the Matlab Answers forum asking whether something similar can be done with Matlab listboxes, i.e. add checkboxes next to each list item. There are actually several alternatives for this and I thought this could be a good opportunity to discuss them:
- The HTML image variant
- MathWorks CheckBoxList
- JIDE’s CheckBoxList
- ActiveX and other alternatives
- Matlab uitable in disguise
A few months ago I wrote about Matlab’s undocumented serialization/deserialization functions, getByteStreamFromArray and getArrayFromByteStream. This could be very useful for both sending Matlab data across a network (thus avoiding the need to use a shared file), as well as for much faster data-save using the -V6 MAT format (save -v6 …).
As a followup to that article, in some cases it might be useful to use ZIP/GZIP compression, rather than Matlab’s proprietary MAT format or an uncompressed byte-stream.
Unfortunately, Matlab’s compression functions zip, gzip and tar do not really help run-time performance, but rather hurt it. The reason is that we would be paying the I/O costs three times: first to write the original (uncompressed) file, then to have zip or its counterparts read it, and finally to save the compressed file. tar is worst in this respect, since it does both a GZIP compression and a simple tar concatenation to get a standard tar.gz file. Using zip/gzip/tar only makes sense if we need to pass the data file to some external program on some remote server, whereby compressing the file could save transfer time. But as far as our Matlab program’s performance is concerned, these functions bring little value.
In contrast to file-system compression, which is what zip/gzip/tar do, on-the-fly (memory) compression makes more sense and can indeed help performance. In this case, we are compressing the data in memory, and directly saving to file the resulting (compressed) binary data. The following example compresses int8 data, such as the output of our getByteStreamFromArray serialization:
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.