- Enclose with " for exact match e.g., "uitable report"
- 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
- JSON-Matlab integration
- Undocumented feature list
- Explicit multi-threading in Matlab part 4
- Desktop (33)
- Figure window (34)
- Guest bloggers (42)
- GUI (109)
- Handle graphics (51)
- Hidden property (32)
- Icons (7)
- Java (132)
- Listeners (16)
- Memory (12)
- Mex (9)
- Presumed future risk (260)
- Semi-documented feature (6)
- Semi-documented function (31)
- Stock Matlab function (95)
- Toolbox (2)
- UI controls (36)
- Uncategorized (9)
- Undocumented feature (124)
- 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 (1 day 2 hours ago): Sounds reasonable. You’re welcome to try. Come back and post a followup comment here after you figure out the exact specifics.
- Peter Dowell (1 day 2 hours ago): @Yair, Thanks for the quick reply. I guess the states of the renderers/editors would have to be updated when the user changes the grouping. I was thinking that the...
- Hugo Mendonça (1 day 4 hours ago): Hi, Yair! First of all, I have to say I have read a few topics of your website and it is amazing! Congrats for it and all your effort to make it possible! I am a beginner with...
- Kai (1 day 15 hours ago): Eventually I use UpdatePreviewWindowFcn to convert each video frame image into a Java Image object. I also override paintComponent() of JPanel to setup the Image object as the...
- Yair Altman (2 days 20 hours ago): @Mike – indeed: in this specific example the close-button will only close tab2. You need to set a similar ActionPerformedCallback for any close button that you create.
- Yair Altman (2 days 20 hours ago): @Peter – indeed: when you add a grouping column you affect the model and the old definitions are forgotten. You can do the following (a variant can be found in the...
- Yair Altman (2 days 20 hours ago): @Nike – this sounds on the face of it to be a bug, since the behavior is indeed documented. I suggest that you contact MathWorks (email@example.com) to report this...
- Peter Dowell (3 days 7 hours ago): Hi Yair, I’ve come accross an interesting issue running TreeTable. When I sub group a column either interactively, or programatically as described above, columns which...
- Nike Dattani (3 days 7 hours ago): It looks like there’s been another backwards in-compatibility introduced in R2014a (if not earlier). xlsread('data.xlsx',1) used to read sheet 1 of data.xlsx Now it...
- Mike (3 days 18 hours ago): I guess I wasn’t clear. What I was asking is how do you implement tho son a uitab grouped with uitabgroup to close SelectedTab. it looks like you enter set(jCloseButton,...
- Yair Altman (4 days 2 hours ago): @Mike – I posted this code on this very page, so why would you think it was not possible??? If you’re not sure how it works, simply run it in your Matlab. It...
- Mike (4 days 3 hours ago): Is it possible to implement this on a MATLAB uitabgroup? % First let's load the close icon jarFile = fullfile(matlabroot,'/java/jar /mwt.jar'); iconsFolder =...
- Yair Altman (4 days 3 hours ago): @Masoud – an example for using a node-selection callback was shown in the second article of this series.
- Yair Altman (4 days 3 hours ago): You can add/remove specific nodes in run-time. See the previous article of this series, or in my book (section 4.2) for details.
- Yair Altman (4 days 3 hours ago): @Rick – this is possibly an internal Matlab bug. Try using an explicit font name (e.g. ‘Courier New’) or use HTML formatting in your string (e.g., ‘my...
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:
Last December, I hosted an article on plot.ly‘s awesome online plotting interface for Matlab. Today, I wish to welcome Andrey Dimitrov, a plot.ly engineer, who will discuss generating Plotly graphs in IPython Notebooks, all from within Matlab. The Plotly idea, of generating online interactive graphs, is radically different from Matlab’s standard static plots and deserves a second look. Note that Plotly is a commercial venture.
The Plotly Matlab API enables leveraging the power of Plotly graphs conveniently from your Matlab environment, meaning that you can make a graph like this in Matlab:
You can work in Matlab, share your graphs and data, and then edit it together online. It’s all backed up, collaborative, and can be edited with other APIs and the GUI. It’s free, embeddable, and keeps data and graphs together.
The API provides a set of functions that structure your data and send it to Plotly. All editing can be done through the API or the online GUI. In addition, the Plotly wrapper function can take away all the hassle of learning a new graphing syntax. Just build your figures in Matlab as usual and send them to Plotly with one call to the wrapper.
This notebook goes through some usage scenarios to get Matlab users started with the tool. A version of this post (with several more examples) was also written as an IPython Notebook. Some steps are specific to making everything work in an IPython notebook, but you can always skip through those and get to the good stuff.
Exactly five years ago, in one of this blog’s first articles, I wrote an article explaining how to trap the standard Java Swing events in Matlab, in order to achieve much more extensive behavior customizability than Matlab’s standard uicontrols. Three years ago I wrote a related article showing how to trap any Java events as simple Matlab callbacks, even those from custom Java classes. This worked very well over the years, until now.
Over the past few weeks, several people have commented and emailed me about a problem that occurs in R2014a but not in earlier releases: Whereas Java objects in R2013b and earlier automatically exposed their public events as Matlab callbacks, they no longer do so in R2014a.
As a simple example to illustrate the point, let’s take a simple Java JButton and modify its label when the mouse moves over it, using the
mouseEntered, mouseExited events. In R2013b and earlier this was pretty easy:
% Create and display a simple JButton jButton = javax.swing.JButton('click me'); javacomponent(jButton, [50,50,80,20], gcf); % Set the Matlab callbacks to the JButton's events set(jButton, 'MouseEnteredCallback', @(h,e)set(jButton,'Text','NOW !!!')) set(jButton, 'MouseExitedCallback', @(h,e)jButton.setText('click me'))
(Note how I used two distinct variants to set the button’s text, using either the Matlabized Text property with the set function, or the standard JButton’s setText() method. We can use either of these variants: they are equivalent and largely a matter of personal taste, although the Java method is generally preferable.)
Unfortunately, in R2014a we get an error, since the
jButton object no longer exposes its events as Matlab callbacks:
>> set(jButton, 'MouseEnteredCallback', @(h,e)set(jButton,'Text','NOW !!!')) The name 'MouseEnteredCallback' is not an accessible property for an instance of class 'javax.swing.JButton'.
Matlab includes a wide variety of internal widgets (GUI components) that could be very useful in our GUIs. One such widget is an animated spinning icon, which is often used by Matlab itself and numerous toolboxes to illustrate a running task:
One of the internal widgets that are readily-available for use in our Matlab GUI and displays a similar (but not identical) spinning icon is
BusyAffordance, which is included in the built-in
BusyAffordance creates a visible panel with an animated spinning icon and optional text label as long as the panel’s object is in the “started” mode (the mode can be started/stopped numerous times).
The usage is very simple: Continue reading