Useful links
Recent Posts
- Improving graphics interactivity
- Interesting Matlab puzzle – analysis
- Interesting Matlab puzzle
- Undocumented plot marker types
- Matlab toolstrip – part 9 (popup figures)
- Matlab toolstrip – part 8 (galleries)
- Matlab toolstrip – part 7 (selection controls)
- Matlab toolstrip – part 6 (complex controls)
- Matlab toolstrip – part 5 (icons)
- Matlab toolstrip – part 4 (control customization)
- Reverting axes controls in figure toolbar
- Matlab toolstrip – part 3 (basic customization)
- Matlab toolstrip – part 2 (ToolGroup App)
- Matlab toolstrip – part 1
- Matlab callbacks for uifigure JavaScript events
Archives
Categories
- Desktop (45)
- Figure window (59)
- Guest bloggers (65)
- GUI (165)
- Handle graphics (84)
- Hidden property (42)
- Icons (15)
- Java (174)
- Listeners (22)
- Memory (16)
- Mex (13)
- Presumed future risk (393)
- Public presentation (6)
- Semi-documented feature (10)
- Semi-documented function (35)
- Stock Matlab function (139)
- Toolbox (9)
- UI controls (52)
- Uncategorized (13)
- Undocumented feature (217)
- Undocumented function (37)
Tags
AppDesigner 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 Semi-documented feature Semi-documented function Toolbar Toolstrip uicontrol uifigure UIInspect uitools Undocumented feature Undocumented function Undocumented propertyRecent Comments
- Antonio (17 days 8 hours ago): Note Intel’s MKL implementations of BLAS/LAPACK are already multi-threaded, so it is likely that you won’t achieve a vast improvement (the overall performance could even...
- Testing 123 (254 days 23 hours ago): The mexCallMATLAB function raises interesting possibilities for parallelization. I spent a few hours (days…) writing a mexFunction that mimics the functionality of...
- Andrés Aguilar (393 days 22 hours ago): Hello, has anyone tried to change the language of the DateComboBox? For example English -> French ———— 8212;—- January -> Janvier April...
- Yair Altman (686 days 1 hour ago): You didn’t say what code you were trying to run and what exactly didn’t work for you, so I cannot help you. In general I can say that the code still works ok on...
- Nicholas (687 days 23 hours ago): It has come to my attention that in R2022a the content for HtmlText and CurrentLocation cannot be set for an HTMLBrowserPanel object. This can be easily demonstrated in R2022a with...
- bryan taylor (689 days 5 hours ago): I’m using matlab r2021a. The command above don’t seem to work. The commands run without error but the changes do no effect the text box.
- Yair Altman (706 days 6 hours ago): @Mitchell – in most cases the user wants a single string identifier for the computer, that uniquely identifies it with a distinct fingerprint that is different from any other...
- Mitchell (706 days 15 hours ago): Great post! I’m not very familiar with the network interfaces being referenced here, but it seems like the java-based cross-platform method concatenates all network...
- Eric (755 days 4 hours ago): this save me a lot of time, thank you very much!
- Yair Altman (1077 days 0 hours ago): You forgot to wait for the figure and the axes to fully render. After a pause and drawnow the code works ok (at least in R2021a).
- Anton Semechko (1077 days 0 hours ago): Hi, Yair, Do you know why is it impossible to add listeners to any of the uiobjects in UIFigure? And is there some way to work around this limitation? For example:...
- Davide Coluzzi (1111 days 23 hours ago): Hello, I have just found out this website and the book, they are great! Many thanks for this. I am developing a GUI in Matlab and I added a uiimage to receive info, so that...
- Yair Altman (1229 days 5 hours ago): drawnow is the entry function to Matlab graphics updates, which chains numerous internal drawing/painting calls, as well as associated callbacks (internal and user-defined)....
- Ondrej (1229 days 5 hours ago): Hi Yair, I have a slightly off-topic question. I am using Matlab 2017a and when using drawnow to refresh the graphics I noticed in the profiler (with -memory option on) this: Code...
- Florian (1460 days 13 hours ago): Hello Yair thank you for this work. I am trying to using the sorting function but obviously it treat my ‘double’ as ‘string’. Then the sorting of the numbers...
Contact Us
String/char compatibility
In numerous functions that I wrote over the years, some input arguments were expected to be strings in the old sense, i.e. char arrays for example, 'on'
or 'off'
. Matlab release R2016b introduced the concept of string objects, which can be created using the string function or [starting in R2017a] double quotes ("on"
).
The problem is that I have numerous functions that supported the old char-based strings but not the new string objects. If someone tries to enter a string object ("on"
) as input to a function that expects a char-array ('on'
), in many cases Matlab will error. This by itself is very unfortunate – I would have liked everything to be fully backward-compatible. But unfortunately this is not the case: MathWorks did invest effort in making the new strings backward-compatible to some degree (for example, graphic object property names/values and many internal functions that now accept either form as input). However, backward compatibility of strings is not 100% perfect.
In such cases, the only solution is to make the function accept both forms (char-arrays and string objects), for example, by type-casting all such inputs as char-arrays using the builtin char function. If we do this at the top of our function, then the rest of the function can remain unchanged. For example:
Continue reading
Blocked wait with timeout for asynchronous events
Readers of this website may have noticed that I have recently added an IQML section to the website’s top menu bar. IQML is a software connector that connects Matlab to DTN’s IQFeed, a financial data-feed of live and historic market data. IQFeed, like most other data-feed providers, sends its data in asynchronous messages, which need to be processed one at a time by the receiving client program (Matlab in this case). I wanted IQML to provide users with two complementary modes of operation:
- Streaming (asynchronous, non-blocking) – incoming server data is processed by internal callback functions in the background, and is made available for the user to query at any later time.
- Blocking (synchronously waiting for data) – in this case, the main Matlab processing flows waits until the data arrives, or until the specified timeout period has passed – whichever comes first.
Implementing streaming mode is relatively simple in general – all we need to do is ensure that the underlying connector object passes the incoming server messages to the relevant Matlab function for processing, and ensure that the user has some external way to access this processed data in Matlab memory (in practice making the connector object pass incoming data messages as Matlab callback events may be non-trivial, but that’s a separate matter – read here for details).
In today’s article I’ll explain how we can implement a blocking mode in Matlab. It may sound difficult but it turns out to be relatively simple.
I had several requirements/criteria for my blocked-wait implementation:
- Compatibility – It had to work on all Matlab platforms, and all Matlab releases in the past decade (which rules out using Microsoft Dot-NET objects)
- Ease-of-use – It had to work out-of-the-box, with no additional installation/configuration (which ruled out using Perl/Python objects), and had to use a simple interface function
- Timeout – It had to implement a timed-wait, and had to be able to tell whether the program proceeded due to a timeout, or because the expected event has arrived
- Performance – It had to have minimal performance overhead
Categories: Java, Listeners, Low risk of breaking in future versions, Memory
Tags: Java, Performance
Leave a comment
Tags: Java, Performance
Leave a comment
Speeding-up builtin Matlab functions – part 2
Last week I showed how we can speed-up built-in Matlab functions, by creating local copies of the relevant m-files and then optimizing them for improved speed using a variety of techniques. Today I will show another example of such speed-up, this time of the Financial Toolbox’s maxdrawdown function, which is widely used to estimate the relative risk of a trading strategy or asset. One might think that such a basic indicator would be optimized for speed, but experience shows otherwise. In fact, this function turned out to be the main run-time performance hotspot for one of my clients. The vast majority of his code was optimized for speed, and he naturally assumed that the built-in Matlab functions were optimized as well, but this was not the case. Fortunately, I was able to create an equivalent version that was 30-40 times faster (!), and my client remains a loyal Matlab fan.
In today’s post I will show how I achieved this speed-up, using different methods than the ones I showed last week. A combination of these techniques can be used in a wide range of other Matlab functions. Additional speed-up techniques can be found in other performance-related posts on this website, as well in my book Accelerating MATLAB Performance.
Continue reading
Categories: Low risk of breaking in future versions, Stock Matlab function, Toolbox
Tags: Performance, Pure Matlab, Toolbox
8 Comments
Tags: Performance, Pure Matlab, Toolbox
8 Comments
Speeding-up builtin Matlab functions – part 1
A client recently asked me to assist with his numeric analysis function – it took 45 minutes to run, which was unacceptable (5000 runs of ~0.55 secs each). The code had to run in 10 minutes or less to be useful. It turns out that 99% of the run-time was taken up by Matlab’s built-in fitdist function (part of the Statistics Toolbox), which my client was certain is already optimized for maximal performance. He therefore assumed that to get the necessary speedup he must either switch to another programming language (C/Java/Python), and/or upgrade his computer hardware at considerable expense, since parallelization was not feasible in his specific case.
Luckily, I was there to assist and was able to quickly speed-up the code down to 7 minutes, well below the required run-time. In today’s post I will show how I did this, which is relevant for a wide variety of other similar performance issues with Matlab. Many additional speed-up techniques can be found in other performance-related posts on this website, as well in my book Accelerating MATLAB Performance.
Continue reading
Categories: Low risk of breaking in future versions, Stock Matlab function, Toolbox
Tags: Performance, Pure Matlab, Toolbox
7 Comments
Tags: Performance, Pure Matlab, Toolbox
7 Comments
Spicing up the Matlab Editor
I’d like to introduce guest blogger Andreas Justin, who will discuss some way-cool features in his Editor Plugin utility. Many of his feature implementations are not Editor-specific and can be reused in other Matlab-Desktop applications, for example dockable panels, and integration with Matlab’s main Preferences window.
Note: I will be traveling to the USA in June, and to Spain in August. If you would like me to visit your location for onsite consulting or training, then please let me know.
Happy Easter/Passover!
Overview
Compared to other IDE like IntellIJ, Eclipse and many more, Matlab’s editor seems somewhat outdated. Especially writing Object-Oriented code in Matlab is kind of a hassle. To make Matlab more user friendly, I’ve written a Java app that adds important features to the editor – Features such as navigating inside Class-code and in Inherited members; Searching through methods and instantly jumping to desired location; Reopening an editor that was closed by accident; Storing bookmarks between Matlab sessions; and Live Templates using commands directly written in the editor, replaced by pre-defined text.The default Keyboard shortcuts listed below for the features can be customized. Most variables can be customized as well (I will point out which variables are not [yet] customizable).
Most GUIs have a search field. Within this search field you can move the list or the tree up and down using arrow keys, or hit <escape> to return to editor. These search fields allow you to enter regular expressions to limit results shown in list or tree. Also, most GUIs are dockable.
The Editor-Plugin utility is open-source. It is available on GitHub and also mirrored on the Matlab File Exchange. A detailed setup guide is provided on the utility’s wiki section in GitHub.
If you discover any problem or have any suggestion for improvement, please visit the utility’s Issues section on GitHub, where all open/closed issues can be tracked and discussed.
A brief overview of some of the features is presented below. For a detailed explanation of these and other features (which are not listed below), please review the Features section of the utility’s wiki (you guessed it: on GitHub…).
Editing
- Delete / duplicate lines – CTRL + SHIFT + Y or D allows you to delete or duplicate current line.
- Move lines up or down – CTRL + ALT + UP or DOWN allows you to move selected lines up or down.
- Live (auto-replace) templates – Live Templates are editor commands you can design to insert predefined code. Here’s an example for the command
%this%
(delivered within the package). When you type a command into the editor the string will get replaced by the predefined text. This predefined text may include variables depending on what you want to achieve.%this%
was designed to insert the fully qualified name of the current class you’re in (or function, or script). - Clipboard Stack – CTRL + SHIFT + V opens the Clipboard Stack, where the last 10 copied/cut text are stored and can be directly inserted into the current editor position. The Clipboard Stack only stores text copied from editor.
Categories: Desktop, Guest bloggers, High risk of breaking in future versions, Java, Undocumented feature
Tags: Andreas Justin, Desktop, Editor, Java
Leave a comment
Tags: Andreas Justin, Desktop, Editor, Java
Leave a comment
Adding custom properties to GUI objects
Matlab objects have numerous built-in properties (some of them publicly-accessible/documented and others not, but that’s a different story). For various purposes, it is sometimes useful to attach custom user-defined properties to such objects. While there was never a fully-documented way to do this, most users simply attached such properties as fields in the UserData property or the object’s [hidden] ApplicationData property (accessible via the documented setappdata/getappdata functions).
An undocumented way to attach actual new user-defined properties to objects such as GUI handles or Java references has historically (in HG1, up to R2014a) been to use the undocumented schema.prop function, as I explained here. As I wrote in that post, in HG2 (R2014b onward), we can use the fully-documented addprop function to add new custom properties (and methods) to such objects. What is still NOT documented, as far as I could tell, is that all of Matlab’s builtin handle graphics objects indirectly inherit the dynamicprops
class, which allows this. The bottom line is that we can dynamically add custom properties in run-time to any HG object, without affecting any other object. In other words, the new properties will only be added to the handles that we specifically request, and not to any others.
All this is important, because for some unexplained reason that escapes my understanding, MathWorks chose to seal its classes, thus preventing users to extend them with sub-classes that contain the new properties. So much frustration could have been solved if MathWorks would simply remove the Sealed class meta-property from its classes. Then again, I’d have less to blog about in that case…
Anyway, why am I rehashing old news that I have already reported a few years ago?
Well, first, because my experience has been that this little tidbit is [still] fairly unknown by Matlab developers. Secondly, I happened to run into a perfect usage example a short while ago that called for this solution: a StackExchange user asked whether it is possible to tell a GUI figure’s age, in other words the elapsed time since the figure was created. The simple answer would be to use setappdata with the creation date whenever we create a figure. However, a “cleaner” approach seems to be to create new read-only properties for the figure’s CreationTime and Age:
Continue reading
Categories: GUI, Handle graphics, Java, Low risk of breaking in future versions, Undocumented feature
Tags: GUI, Java, Pure Matlab, schema.prop, Undocumented feature
5 Comments
Tags: GUI, Java, Pure Matlab, schema.prop, Undocumented feature
5 Comments
IP address input control
A few weeks ago, a user posted a question on Matlab Answers, asking whether it is possible to implement a text input control that accepts and validates an IP address (for example, ‘192.168.1.101’). While doing this using purely documented Matlab code is indeed possible (for those of us who are masochistically inclined and/or have nothing else to do with their spare time), a very simple-to-use and polished-looking solution is to use an undocumented built-in Matlab control.
The solution is based on the fact that Matlab comes with a huge set of professional Java-based controls by JideSoft, bundled in various JAR libraries within the %matlabroot%/java/jarext/jide Matlab installation folder. For our specific purposes (an IP-address entry/display control), we are interested in the com.jidesoft.field.IPTextField
control (online documentation), which is part of the JIDE Grids library (%matlabroot%/java/jarext/jide/jide-grids.jar). We can use it as follows:
jIPField = com.jidesoft.field.IPTextField('255.255.255.0'); % set default IP [jIPField, hContainer] = javacomponent(jIPField, [10,10,120,20], hParent); % hParent: panel/figure handle
You can modify the position/size of the text-field in the javacomponent call above, or by modifying the Position / Units properties of the returned hContainer
.
We can retrieve the IP text/numeric values using:
vals = jIPField.getValue'; % 1x4 uint32 array => [255,255,255,0] vals = cell(jIPField.getRawText)'; % 1x4 string cells => {'255','255','255','0'} ip = char(jIPField.getText); % entire IP string => '255.255.255.0'
The IPTextField
component auto-validates the IP values, ensuring that the displayed IP is always valid (for example, IP components cannot be negative or larger than 255). The component has many other features, including the ability to enable/disable, color or format the IP components etc.
Continue reading
Categories: GUI, High risk of breaking in future versions, Java, UI controls, Undocumented feature
Tags: GUI, Java, JIDE, Undocumented feature
3 Comments
Tags: GUI, Java, JIDE, Undocumented feature
3 Comments
Customizing axes tick labels
In last week’s post, I discussed various ways to customize bar/histogram plots, including customization of the tick labels. While some of the customizations that I discussed indeed rely on undocumented properties/features, many Matlab users are not aware that tick labels can be individually customized, and that this is a fully documented/supported functionality. This relies on the fact that the default axes TickLabelInterpreter property value is 'tex'
, which supports a wide range of font customizations, individually for each label. This includes any combination of symbols, superscript, subscript, bold, italic, slanted, face-name, font-size and color – even intermixed within a single label. Since tex is the default interpreter, we don’t need any special preparation – simply set the relevant X/Y/ZTickLabel string to include the relevant tex markup.
To illustrate this, have a look at the following excellent answer by user Ubi on Stack Overflow:
plot(1:10, rand(1,10)) ax = gca; % Simply color an XTickLabel ax.XTickLabel{3} = ['\color{red}' ax.XTickLabel{3}]; % Use TeX symbols ax.XTickLabel{4} = '\color{blue} \uparrow'; % Use multiple colors in one XTickLabel ax.XTickLabel{5} = '\color[rgb]{0,1,0}green\color{orange}?'; % Color YTickLabels with colormap nColors = numel(ax.YTickLabel); cm = jet(nColors); for i = 1:nColors ax.YTickLabel{i} = sprintf('\\color[rgb]{%f,%f,%f}%s', cm(i,:), ax.YTickLabel{i}); end
In addition to 'tex'
, we can also set the axes object’s TickLabelInterpreter to 'latex'
for a Latex interpreter, or 'none'
if we want to use no string interpretation at all.
As I showed in last week’s post, we can control the gap between the tick labels and the axle line, using the Ruler object’s undocumented TickLabelGapOffset, TickLabelGapMultiplier properties.
Also, as I explained in other posts (here and here), we can also control the display of the secondary axle label (typically exponent or units) using the Ruler’s similarly-undocumented SecondaryLabel property. Note that the related Ruler’s Exponent property is documented/supported, but simply sets a basic exponent label (e.g., '\times10^{6}'
when Exponent==6) – to set a custom label string (e.g., '\it\color{gray}Millions'
), or to modify its other properties (position, alignment etc.), we should use SecondaryLabel.
Customizing histogram plots
Earlier today, I was given the task of displaying a histogram plot of a list of values. In today’s post, I will walk through a few customizations that can be done to bar plots and histograms in order to achieve the desired results.
We start by binning the raw data into pre-selected bins. This can easily be done using the builtin histc (deprecated) or histcounts functions. We can then use the bar function to plot the results:
[binCounts, binEdges] = histcounts(data); hBars = bar(hAxes, binEdges(1:end-1), binCounts);
Let’s improve the appearance: Continue reading