- Undocumented Matlab - https://undocumentedmatlab.com -

HG's undocumented parameters interface

Posted By Yair Altman On March 14, 2013 | 6 Comments

Continuing last week’s article on Matlab Handle Graphics’ (HG) undocumented Behavior functionality, today I describe another undocumented aspect of HG. Some of the low-level HG functions, such as line and patch receive their input parameters in one of two formats:

HG line created using either of the input formats
HG line created using either of the input formats

  • The regular fully-documented P-V pairs format:
    line('Marker','*', 'MarkerSize',8, 'MarkerEdgeColor','b', ...
         'Color','r', 'XData',1:5, 'YData',3:7)
    
  • An undocumented struct-fields format:
    lineprops.Marker = '*';
    lineprops.MarkerSize = 15;
    lineprops.MarkerEdgeColor = 'b';
    lineprops.color = 'r';
    lineprops.xdata = 1:5;
    lineprops.ydata = 3:7;
    line(lineprops)
    


A sample usage of using the input struct technique can be seen (for example) in %matlabroot%/toolbox/matlab/graphics/@graphics/@datatip/datatip.m. I have also used it in my IB-Marlab [1] application, which connects Matlab to Interactive Brokers. The idea is to make it easy for users to use whichever input format they feel more comfortable with: both P-V pairs or struct fields are supported.
Both input formats can be interchanged and mixed. We should just note that the latest (right-most) property is updated last and overrides any previous value of the same property. Here are some examples — can you guess the results of each of them?

line(lineprops, 'MarkerEdgeColor','g', 'MarkerSize',7)
line('MarkerEdgeColor','g', lineprops, 'MarkerSize',7)
line('MarkerEdgeColor','g', 'MarkerSize',7, lineprops)
line(1:4, 2:5, 'MarkerEdgeColor','g', 'MarkerSize',7, lineprops)
line(1:4, 2:5, 'MarkerEdgeColor','g', lineprops, 'MarkerSize',7)
line(1:4, 2:5, lineprops, 'MarkerEdgeColor','g', 'MarkerSize',7)

While the struct format is documented (although not widely-used) in the set function, it is not documented for line, patch and other functions. Nor is it documented that we can mix the struct format with the P-V pairs format.
Note that not all HG functions support the struct input format. For example, line does, but plot does not. I do not understand the reason for this. It would seem reasonable for MathWorks to include this useful struct format for all their HG functions, and since set already supports it the extra effort would have been trivial. Maybe this will still happen in some future Matlab release…

Categories: Handle graphics, Medium risk of breaking in future versions, Stock Matlab function, Undocumented feature


6 Comments (Open | Close)

6 Comments To "HG's undocumented parameters interface"

#1 Comment By Eric On March 14, 2013 @ 08:13

Is this behavior because the functions use INPUTPARSER? If so, perhaps it is true for any function that uses inputparser – I haven’t tested it out to see…

#2 Comment By Yair Altman On March 14, 2013 @ 08:17

@Eric – both line, patch and inputparser are internal functions whose contents are not accessible for us to check.

#3 Pingback By ishghandle’s undocumented input parameter | Undocumented Matlab On March 27, 2013 @ 14:09

[…] Two weeks ago I wrote about Matlab Handle Graphics (HG) undocumented acceptance of structs as input parameter to some plotting functions. Continuing in a related matter, today I expose an undocumented input parameter for HG’s ishghandle function […]

#4 Comment By Miguel Gaspar On April 4, 2013 @ 12:25

I had also noticed this sometime ago. It would seem very useful to use with:

hprops=get(h);
hprops.some_property=...
set(h,hprops)

but unfortunately get(h) returns read-only properties, which result in an error when used with set():

>> set(hl,linep)
??? Error using ==> set
Attempt to modify a property that is read-only.
Object Name :  line
Property Name :  'Annotation'.

Do you know how to overcome this?

#5 Comment By Yair Altman On April 4, 2013 @ 14:30

@Miguel – why not simply pass to set only the changed properties, that you can be certain are settable? For example:

hprops = [];
hprops.some_property1 = ...
hprops.some_property2 = ...
set(h,hprops)

#6 Comment By Miguel Gaspar On April 4, 2013 @ 15:38

Hi, Yair,

Thanks for your reply, and above all, thanks for the continuous stream of pearls you provide…

I would like to use the get() method for two reasons:
– symmetry: it has some aesthetic appeal, feels natural to me;
– laziness: it would allow the use of auto-complete…

not very strong reasons, specially since recent versions of Matlab provide auto-complete of property names in set/get, but still prompted me to write this function:

function hprops=getset(h)
% Returns only the settable properties for the handle graphics object h, so
% that it can be used directly in the form:
% set(h,hprops)

fn=fieldnames(set(h(1)));
fv=get(h,fn);
hprops=cell2struct(fv',fn,1);

Unfortunately, although this works for a vector of handles, of same type, returning a vector struct, this can’t be used in set(...):

hlines=findobj(gca,'Type','line');
lineprops=getset(hlines);
lineprops(1).LineStyle=':';
lineprops(2).LineStyle='-';
set(hlines,lineprops)

since only the last value specified for each property is used, and applied to all handle graphics objects…

A syntax for assigning different values to properties of multiple objects is available for the plot(...) function, which accepts the following:

plot(1:4, [2,4,5,8], 'r',...
     1:4, [3 2 1 4], 'g')

My preferred way of working with handle graphics is to use hg objects instead of handles, although in some cases it requires some additional steps:

hline=hg.line('XData',1:4,'YData',[2,4,5,8]);
hline.Visible=false;
hline.XData=[hline.XData,5];
hline.YData=[hline.YData,3];
hline.Visible=true;
% or:
[hline.XData,hline.YData]=deal([hline.XData,5],[hline.YData,3]);

Which method (set(…) with PV pairs, set(…) with struct, or hg objects) do you prefer?


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/hgs-undocumented-parameters-interface

URLs in this post:

[1] IB-Marlab: http://undocumentedmatlab.com/ib-matlab/

[2] Undocumented plot marker types : https://undocumentedmatlab.com/articles/undocumented-plot-marker-types

[3] cellfun – undocumented performance boost : https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost

[4] tic / toc – undocumented option : https://undocumentedmatlab.com/articles/tic-toc-undocumented-option

[5] Matlab-Latex interface : https://undocumentedmatlab.com/articles/matlab-latex-interface

[6] JMI – Java-to-Matlab Interface : https://undocumentedmatlab.com/articles/jmi-java-to-matlab-interface

[7] Undocumented view transformation matrix : https://undocumentedmatlab.com/articles/undocumented-view-transformation-matrix

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.