Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using abbreviations for parameter-value input pairs - why does cellfun's `'UniformOutput', false` work as `'un', 0`?

Tags:

matlab

As a frequent user of cellfun(..., 'UniformOutput', false), I was surprised to discover that the latter two arguments could be input as cellfun(..., 'un', 0). This has immediately shortened my code. My question is, are there any another similar shortcuts/aliases out there?

This doesn't appear to be documented on the MathWorks website. Naughty!

like image 809
Derek Avatar asked Sep 18 '14 08:09

Derek


2 Answers

Background

The property part of a Property-value pair can be often be shortened to the beginning of the property string so long as it is still uniquely identifiable (see examples below). They are also often not case sensitive.
As far as I know this is not documented for anything other than figure properties, where it is very briefly documented.

However this behaviour is also implemented in many The MathWorks functions which take property-value pair arguments, and also in some functions which take string arguments with no value pair.

The problems become what other properties are there...

  • For some functions is not well documented... (and many are either built-in or obfuscated .p files, so no checking the source).
  • Most importantly we cannot know if future versions will introduce new properties.

The use of shortened property names is advised against for this reason, to quote

"using the full property name can prevent problems with futures releases of MATLAB if a shortened name is no longer unique because of the addition of new properties." - Matlab documentation

"Don't shorten your code with P-V abbreviations, it's not worth it, trust me." – Sam Roberts

The brief slightly technical side - This functionality is implemented by default by Matlabs inputParser, however it can be disabled so these abbreviations are by no means guaranteed to work on every function. Only those which use the inputParser in this way or are specifically written to allow this.


Examples using figure properties

  1. Simple Shortening

The following

hFig = figure();
get(Hfig,'Visible')
ans =

on

can be shortened to

hFig = figure();
get(Hfig,'v')
ans =

on

as it is the only property beginning with 'v' (note that 'vi','vis', etc... also work)

  1. Uniqueness

However other properties which start similarly e.g. CurrentAxes, CurrentCharacter & CurrentObject

get(Hfig,'Current')

Error using get
Ambiguous property found.
Object Name: figure
Property Name: 'Current'.

Where as 'currenta','currentc' and 'currento' are uniquely identifiable and would work as intended

  1. Impossible to shorten

A special mention to properties such as Color and ColorMap the name of one is the beginning of another Color can only be used with its full name, due to any shortening being ambiguous with ColorMap 'ColorMap' can be shorted to 'colorm' however, as before.


Counter examples

This section aims to discourage use of shortened property value pairs by showing some of the seemingly unpredictable and unexpected behaviour.

The semi-undocumented hardcopy has some interesting behaviour the documentation states possible options as -dps,-deps,-dps2,-deps2,-dill,-dhpgl

However '-dh' creates an error where as '-di' works as an abbreviated '-dill' sadly there is no checking the source to see why as it is a .p file

Finally cellfun itself doesn't exactly follow there rules as 'u' should theoretically work if following the rules above, however only 'un' and onwards does.

like image 129
RTL Avatar answered Nov 05 '22 12:11

RTL


If a routine uses MATLAB's inputParser functionality then usually any parameter name can be shortened such that it is still unambiguous and is matched case insensitively. The same is true for properties of objects such as get(gcf, 'pos') as a shorter version of get(gcf, 'Position').

However, I don't know if there is any documentation on this usage and I suggest you try it and see.

And 0 is usually equivalent to false (non-zero being true).

PS I would suggest that using these shortcuts is fine at the command line but when writing functions and scripts it is better to use the full names.

like image 36
MikeLimaOscar Avatar answered Nov 05 '22 14:11

MikeLimaOscar