- Enclose with " for exact match e.g., "uitable report"
- Advanced on-site Matlab training - amazing value
- Solving an mput (FTP) hang problem
- Accessing internal Java class members
- Static Java classpath hacks
- Using linkaxes vs. linkprop
- Matlab designs by Tim Smith
- Capturing print events
- JGraph in Matlab figures
- Some performance-tuning tips
- Sliders in Matlab GUI
- Persisting transparent colors in HG2
- Undocumented HG2 graphics events
- copyobj behavior change in HG2
- General-use object copy
- Auto-completion widget
- Undocumented view transformation matrix
- Desktop (35)
- Figure window (37)
- Guest bloggers (49)
- GUI (124)
- Handle graphics (67)
- Hidden property (35)
- Icons (7)
- Java (144)
- Listeners (19)
- Memory (14)
- Mex (9)
- Presumed future risk (302)
- Semi-documented feature (7)
- Semi-documented function (31)
- Stock Matlab function (117)
- Toolbox (4)
- UI controls (41)
- Uncategorized (10)
- Undocumented feature (153)
- Undocumented function (30)
TagsActiveX Callbacks COM Compiler Desktop Donn Shull Editor Figure FindJObj GUI GUIDE Handle graphics HG2 Hidden property HTML Icons Internal component Java JavaFrame JIDE JMI Listener Malcolm Lidierth MCOS Memory Menubar Mex Optical illusion 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 (2 days 12 hours ago): @Mathieu – try to run findjobj after your figure is made visible if you wish to avoid the flicker
- Mathieu G (2 days 15 hours ago): Hello! Thanks for your post. It is very helpful! I don’t know the java language but I try to modify a button of my GUI (using GUIDE) to a borderless button. So I tried to...
- Ange Kouemo (3 days 15 hours ago): Hi Yair, I am using Matlab 2014b and created a Uitable that I want to display in a Jpanel. I tried to get the underlying Java object handle of the uitable using the functions...
- michael r (3 days 18 hours ago): Hi If I using DefaultCheckBoxNode to create CheckBoxTree and I want to change the icon of the leaf and still having the checkbox on it. How do I do it ?
- Dave Brown (6 days 12 hours ago): I ran into a problem from using java 7 that took a while to discover so I thought I’d share it. Hopefully it helps those with the same problem in the future. jScrollbar =...
- jinu (8 days 19 hours ago): nice writeup! about a year ago i moved to python/cython because of how slow matlab gui’s were with large datasets. i wish i had known some of your tricks back then. will...
- Yair Altman (9 days 23 hours ago): @Paul & @Yvon – in HG2 (R2014b onward), you can simply set the Focus events (FocusGained, FocusLost etc.) on the direct child of the AxisComponent, as follows:...
- Yvon Halbwachs (10 days 0 hours ago): Paul, I’m facing the same problem as you have in R2014b. By any chance, did you find any solution to this? Yvon
- Nimrod Geffen (10 days 20 hours ago): Shalom Yair! I have a GUI that I would like to have the interactive option of deleting scatter points with Brush function. My code is: h = brush; set(h, 'Enable', 'on',...
- Yair Altman (10 days 20 hours ago): Because of the EDT. Also read this: http://undocumentedmatlab.com/ blog/solving-a-matlab-hang-pro blem
- Fred Vanner (10 days 20 hours ago): Thanks! That works like a dream! (Why?)
- Yair Altman (10 days 20 hours ago): I suggest that you add a call to drawnow; pause(0.1); after the grid creation and before requesting XGridHandle: x=linspace(-1,1); fx=x.^3; pfx=plot(x,fx); grid on; ax=gca;...
- Fred Vanner (10 days 21 hours ago): I’m having problems with XGridHandle (etc.) in scripts / functions (I’ve had no problems from the command line). Example: x=linspace(-1,1); fx=x.^3; pfx=plot(x,fx);...
- C. Chu (11 days 9 hours ago): I think I found a bug in propertiesGUI. If you have multiple field names that exist in different structs, all of those property fields are set to the same type as the last field....
- Adam (11 days 9 hours ago): Keeps showing Java exception errors in the Command Window. I have added some null checks into the editboxValidation function but couldn’t stop the error messages being displayed....
Matlab includes a variety of builtin utility functions that enable easy access to FTP. These functions are basically methods of an FTP object using Matlab’s old class system (for example, the mput function is implemented as mput.m in the %matlabroot%\toolbox\matlab\iofun\@ftp\ folder). These are pretty old files that haven’t changed much in years.
I recently needed to upload files from Matlab onto an FTP server, and discovered that calling mput simply hang Matlab to the point that I needed to kill and restart the Matlab process. The problem was not in the FTP server, since it could be accessed normally using FTP clients such as FileZilla and WinSCP. So it had to be something internal to Matlab. My initial workaround was to create an automation script (using WinSCP in my case) for the file upload. But this kludge is bot non-robust as well as slow. A fix to the Matlab problem would be much better.
Some online research yielded others who have complained about similar issues over the years, but I saw no concrete answer. I saw many references online to problems that relate to the combination of passive FTP with Windows 7 / firewall / Java 7, that suggested several fixes (example 1, example 2, example 3, example 4). However, none of them solved the problem: calling mput (or dir etc.) continued to freeze Matlab, forcing either a hard process kill or waiting several minutes for the timeout.
Today, Malcolm Lidierth, author of the Waterloo graphics package, told me that he also saw a similar situation on Mac. Fortunately, Malcolm discovered a much better solution to the problem than my external scripts workaround. It seems that simply setting the Java object underlying Matlab’s FTP object to use passive mode fixes the problem (possibly in addition to the other fixes mentioned above).
Following my previous post on using the Java classloader, I thought I’d follow up today with a somewhat-related peculiarity.
Whenever we have an enum or inner-class defined within a Java class, it can be accessed in Java using standard dot-notation. For example,
com.mathworks.hg.peer.ComboboxPeer.MLComboBox refers to an inner class
MLComboBox which is defined within the
ComboboxPeer class. When compiled, the class would be stored in a file called ComboboxPeer$InnerClassName.class. In other words, the JVM uses the $ separator char to indicate that MLComboBox is internal to ComboboxPeer.
Within the Java code, we would simply use the regular dot-notation (
ClassName.MLComboBox) – the $ separator is part of the internal JVM implementation that the Java programmer should not be concerned about.
Unfortunately, we cannot ignore this in Matlab: Matlab’s interpreter, which acts as a bridge to the JVM, is not smart enough to know that in certain cases the dot-notation should be converted into a $. Therefore, trying to access
ClassName.MLComboBox directly in Matlab fails:
>> jObject = com.mathworks.hg.peer.ComboboxPeer.MLComboBox() Undefined function or variable 'MLComboBox'. >> jObject = com.mathworks.hg.peer.ComboboxPeer$MLComboBox() jObject = com.mathworks.hg.peer.ComboboxPeer$MLComboBox() ↑ Error: The input character is not valid in MATLAB statements or expressions.
The solution in such cases is to use Matlab’s javaObject (or javaObjectEDT) function with the JVM’s internal $-representation:
>> jObject = javaObject('com.mathworks.hg.peer.ComboboxPeer$MLComboBox',) jObject = com.mathworks.hg.peer.ComboboxPeer$MLComboBox[,0,0,0x0,invalid,layout=com.mathworks.hg.peer.types.HGWindowsComboBoxUI$1,alignmentX=...]
A few days ago I encountered a situation where I needed to incorporate a JAR file into Matlab’s static Java classpath. I came across several relevant hacks that I thought could be useful for others:
- The documented approach
- Fixing javaclasspath.txt in run-time
- Preloading Java class files in javaclasspath.txt
- Loading Java class files into the static classpath in run-time (!)
- Analyzing loaded Java classes in run-time
The documented approach
In most use-cases, adding Java classes (or ZIP/JAR files) to the dynamic Java classpath is good enough. This can easily be done in run-time using the javaaddpath function. In certain cases, for example where the Java code uses asynchronous events, the Java files need to be added to the static, rather than the dynamic, Java classpath. In other cases, the Java code misbehaves when loaded using Matlab’s dynamic Java classloader, rather than the system classloader (which is used for the static classpath (some additional info). One example of this are the JAR/ZIP files used to connect to various databases (each database has its own Java JDBC connector, but they all need to reside in the static Java classpath to work properly).
Adding class-file folders or ZIP/JAR files to the static Java classpath can be done in several manners: we can update the Matlab installation’s classpath.txt file, or (starting in R2012b) a user-prepared javaclasspath.txt file. Either of these files can be placed in the Matlab (or deployed application’s) startup folder, or the user’s Matlab preferences folder (prefdir). This is all documented.
There are several important drawbacks to this approach:
One of my clients recently asked me to solve a very peculiar problem: He had several axes and was using Matlab’s builtin linkaxes function to link their axis limits. However, it didn’t behave quite the way that he expected. His axes were laid out as 2×2 subplots, and he wanted the two columns to be independently linked in the X axis, and the two rows to be independently linked in the Y axis:
% Prepare the axes ax(1,1) = subplot(2,2,1); ax(1,2) = subplot(2,2,2); ax(2,1) = subplot(2,2,3); ax(2,2) = subplot(2,2,4); % Plot something x = 0 : 0.01 : 10; line(x, sin(x), 'Parent',ax(1,1)); line(x, sin(2*x), 'Parent',ax(1,2)); line(x, cos(x), 'Parent',ax(2,1)); line(x, cos(5*x), 'Parent',ax(2,2)); % Link the relevant axes linkaxes(ax(:,1),'x'); % left column linkaxes(ax(:,2),'x'); % right column linkaxes(ax(1,:),'y'); % top row linkaxes(ax(2,:),'y'); % bottom row
The problem was that the plots didn’t behave as expected: when zooming in on the bottom-left axes, for example, only the bottom-right axes was updated (Y-limits synced), whereas the top-left axes’ X-limits remained unchanged:
Walter Roberson liked this post
Matlab has undergone significant facelifts in recent years: Matlab Mobile (first introduced in 2010, with various upgrades since), R2012b’s new desktop toolstrip, various Matlab Central’s website facelifts (example1, example2), R2014b’s new graphics system (HG2), Matlab on the Web (MOTW), and Matlab’s upcoming GUI framework (AppDesigner). That’s quite a lot of UI designs, new and overhauled, over a relatively short timespan.
Designer Tim Smith (designbytimsmith.com), is apparently responsible for many of the sleek UI designs, working with MathWorks’ internal User Experience (UX) team. Tim’s website showcases his work, philosophy, and design process for Matlab’s AppDesigner, Desktop toolstrip, Matlab Mobile, and Matlab on the Web.
I highly recommend reading what Tim has to say about these designs, as well as other designs that he created for other clients. Impressive work indeed.
Formerly a MathWorks visual designer, Tim left MathWorks in 2012 to join PayPal, and currently works at Google. designbytimsmith.com has no contact page, and Tim is apparently very secretive about his email address, which is a bit odd for someone who was the design lead for Google’s new Inbox product. Anyway, you can contact him via his LinkedIn profile or Google+ page.
If you want to get a feel for what Matlab might look like down the road, simply head over to Tim’s website.
As a followup to my recent post on capturing and using HG2′s new graphic events, I would like to welcome back guest blogger Robert Cumming, who developed a commercial Matlab GUI framework. Today, Robert will highlight an unusual use of event listeners to enable print output customizations by tapping into print-related events.
One of my toolbox clients wanted to ensure that every print-out, of any Matlab GUI, would contain a datestamp footer and a project id header.
Initially I thought this would be a simple task, since the GUI framework already has saveAs and copyToClipboard methods that users could utilize. Moreover, as Yair has shown some years ago, we can easily adapt the standard callbacks associated with the Matlab figure toolbar’s <Print> button and the menu-bar’s Print action. We can even customize the standard print setup. However, all this does not help when directly invoking printout commands from the Matlab command line or from within the user program, for example:
% Create the GUI [x,y,z] = peaks(75); surf(x,y,z); % printout via print() or export_fig print(gcf, .... ) export_fig filename % see http://undocumentedmatlab.com/blog/export_fig
I would like to introduce guest blogger Scott Koch. Scott is part of the development team at Eigenvector Research Inc., makers of PLS_Toolbox, a commercial chemometrics and multivariate data analysis toolbox. Today Scott will expand on JGraph, a Java-based open source graph visualization and diagramming library.
What is JGraph?
Graphing is a powerful visualization tool that has not yet been integrated into the core Matlab product. I assume that this will be rectified by MathWorks in a future Matlab release. But until that time, or even later if you are still using R2015a or older, we do not have a solution in the core Matlab. In our toolbox product, we use a cleverly designed image to display a simple diagram, but I always thought it would be nice to have a “real” diagramming tool that was interactive and full-featured.
So, as a long time follower of Yair’s blog, I was thrilled to see his post several years ago about integrating Java libraries for charting and diagramming in Matlab figures. Unfortunately, my first attempts at getting JGraph to work in Matlab were not very successful. Thanks to a comment on that original post by Markus Behle, I was able to gain full access to JGraph in Matlab. Today’s post will show how we can use JGraph in Matlab.
Today’s post is about performance. My goal is to show that contrary to widespread perception, Matlab is not inherently too slow to be used for real-life programs. In fact, by taking a small amount of time (compared to the overall dev time), Matlab programs can be accelerated by a large factor. I wish to demonstrate this claim with work that I recently completed for the Crustal Dynamics research group at Harvard University. They have created interactive Matlab GUIs for earthquake hazard, visualizing deformation and motion at plate boundary zones, recorded as GPS velocities:
These GUIs served them well for several years. But when they recently tried to analyze larger problems that involved far more data, it was getting so slow that it limited their ability to interrogate the GUI results effectively and do productive science (take a look at all the data points around New Zealand in the screenshot above). This is when I stepped in to help.
One of my consulting clients asked me last week if I knew an easy way to integrate a range (dual-knob) slider control in Matlab GUI. Today’s post is an expansion of the answer I provided him, which I though might interest other Matlab users.
- Matlab vs. Java sliders
- Using JSlider
- Range (dual-knob) sliders
- AppDesigner – Matlab’s new GUI
- Conclusions and some personal musings
Matlab vs. Java sliders
As funny as it may sound, Matlab’s so-called “slider” control (
uicontrol('Style','slider')) is actually implemented as a scroll-bar, rather than the more natural JSlider. I believe that this is due to a design decision that occurred sometime in the 1990′s (sliders were not as prevalent then as they are nowadays). This was never corrected, probably for backward-compatibility reasons. So to this day, Matlab’s so-called “slider” is actually a scroll-bar, and we do not [yet] have a real slider control in standard Matlab, apparently since the ‘slider’ uicontrol style is already in use. Spoiler alert: this will change soon — keep reading.
It gets worse: for some reason Matlab’s implementation of the so-called “slider” uses a Windows95 look-and-feel that makes the control look antique in today’s GUI standards. Using Java Swing’s standard JScrollBar control would at least have made it appear more consistent with the other Matlab controls, which are all based more closely on Java Swing:
% Standard Matlab "slider" uicontrol('style','slider', 'position',[10,10,200,20]); % Standard Java JScrollBar jScrollbar = javax.swing.JScrollBar; jScrollbar.setOrientation(jScrollbar.HORIZONTAL); javacomponent(jScrollbar,[10,40,200,20]); % Standard Java JSlider (20px high if no ticks/labels, otherwise use 45px) jSlider = javax.swing.JSlider; javacomponent(jSlider,[10,70,200,45]);
I advise users of the current Matlab GUI to use
JSlider, rather than Matlab’s standard “slider” uicontrol. The rest of today’s post will discuss the
Several months ago, I showed how we can set semi- and fully-transparent colors in HG2 (Matlab’s new graphics engine, starting in R2014b) for multiple graphic objects, including plot lines, plot markers, and area charts:
hLine = plot([0,150], [-0.5,0.5], 'r'); box off; hold on; ydata = sin(0:0.1:15); hArea = area(ydata); drawnow; pause(0.05); % this is important! hArea.Face.ColorType = 'truecoloralpha'; hArea.Face.ColorData(4) = 40; % 40/255 = 0.16 opacity = 84% transparent
Unfortunately, these settings are automatically overridden by Matlab when the figure is printed, saved, or exported:
% These commands result in an opaque (non-transparent) plot print(gcf); saveas(gcf, 'plot.png');