- Enclose with " for exact match e.g., "uitable report"
- Advanced on-site Matlab training - amazing value
- 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
- Handle object as default class property value
- Plot legend title
- Desktop (35)
- Figure window (37)
- Guest bloggers (49)
- GUI (124)
- Handle graphics (67)
- Hidden property (35)
- Icons (7)
- Java (142)
- Listeners (19)
- Memory (14)
- Mex (9)
- Presumed future risk (300)
- Semi-documented feature (7)
- Semi-documented function (31)
- Stock Matlab function (116)
- Toolbox (4)
- UI controls (41)
- Uncategorized (10)
- Undocumented feature (151)
- 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
- mark mikofski (16 hours 56 minutes ago): I wish I had put more detail into using Newtonsoft JSON.net. Here’s a Gist demo % download JSON.net from https://github.com/JamesNK/New tonsoft.Json/releases % extract...
- Niko (17 hours 16 minutes ago): Hi Yair, Is it possible to have multiple objects share the same uicontextmenu? I’ve tried it but the problem is I don’t know how to figure out which object is right-clicked...
- Sandor Toth (1 day 22 hours ago): Dear Yair, do you know a way to change the XTickLabel vertical position, a.k.a its distance from the x-axis line? The generally suggested solution is to recreate the labels...
- Yair Altman (2 days 15 hours ago): @Christina – sounds like something’s funky with your Java. Java warnings cannot be suppressed. You have a very old Matlab release so try upgrading to a newer release.
- Christina (5 days 18 hours ago): In Matlab2007 I’m trying to use the JSlider but I keep getting a “Warning: Transform Fcn…” error every time the GUI repaints. I cannot find a way to get...
- Ed Yu (5 days 19 hours ago): Yair, I would say with regard to loading classes through the java (root) classloader directly should not be that risky… As long as MATLAB is using Java, the root classloader...
- sebastian (5 days 21 hours ago): Specifying relative paths in classpath.txt may not be supported, but for me it works just fine in R2011b. Simply add paths relative to the location of classpath.txt (i.e. relative...
- Armindo (5 days 21 hours ago): Hi, I try to use the: function moveptr(hAx, x, y) However I get the following error Undefined function ‘getpos’ for input arguments of type ‘matlab.graphics.axis...
- Martin (7 days 1 hour ago): @Mariam – you can also use the -struct flag of the save function, if you want to modify the struct s directly without going through another temporary variable: save('testFig.fig',...
- Yair Altman (7 days 3 hours ago): @Mariam – the save command save the data including the variable name as top-level struct field. You should keep the same top-level field-name as used by the FIG format...
- Mariam (7 days 8 hours ago): I tried it without making any changes to ‘s’. Just loading it s = load('C:\Users\Mariam\Desktop\ fitted response\atlas masks\IPS.fig','-mat'); and re-saving it...
- Yair Altman (7 days 23 hours ago): @Mariam – You probably updated your s data-struct incorrectly
- Mariam (7 days 23 hours ago): I already tried this and this the error I am getting >> save('C:\Users\Mariam\Desktop\ fitted response\atlas masks\IPS_1.fig','s','-mat') >> open('C:\Users\Mariam\Desktop\ fitted...
- mugurg (8 days 2 hours ago): If you want to use LaTeX interpreter of MATLAB, create .eps figures, and get the desired font (like Helvetica) instead of Computer Modern fonts, I think I’ve finally found a...
- Yair Altman (8 days 4 hours ago): @Mariam – you can simply use the save command to save your modified d struct back into the *.fig file, and then open this FIG file normally: save('testFig.fig','d','-ma...
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');
R2014b brought a refreshing new graphics engine and appearance, internally called HG2 (the official marketing name is long and impossible to remember, and certainly not as catchy). I’ve already posted a series of articles about HG2. Today I wish to discuss an undocumented aspect of HG2 that I’ve encountered several times over the past months, and most recently today. The problem is that while in the previous HG1 system (R2014a and earlier) we could add property-change listener callbacks to practically any graphics object, this is no longer true for HG2. Many graphics properties, that are calculated on-the-fly based on other property values, cannot be listened-to, and so we cannot attach callbacks that trigger when their values change.
Property-change listeners in HG1
Take for example my post about setting axes tick labels format from 3 years ago: the idea there was to attach a Matlab callback function to the
PropertyPostSet event of the XTick, YTick and/or ZTick properties, so that when they change their values (upon zoom/pan/resize), the corresponding tick-labels would be reformatted based on the user-specified format:
As a followup to last-week’s post on class-object and generic data copies, I would like to welcome back guest blogger Robert Cumming, who developed a commercial Matlab GUI framework. Today, Robert will highlight a behavior change of Matlab’s copyobj function in HG2.
One of the latest features that was introduced to the GUI Toolbox was the ability to undock or copy panels, that would be displayed in a standalone figure window, but remain connected to the underlying class object:
These panel copies had to remain fully functional, including all children and callbacks, and they needed to retain all connections back to the source data. In the example above I have altered the plot to show that it’s an actual copy of the data, but has separate behavior from the original panel.
To simply undock a uipanel to a new figure, we can simply re parent it by updating its Parent property to the new figure handle. To make a copy we need to utilize the copyobj function, rather than re-parenting. copyobj can be used to make a copy of all graphic objects that are “grouped” under a common parent, placing their copy in a new parent. In HG2 (R2014b onwards) the default operation of copyobj has changed.