MATLAB: Magic strings and numbers in Matlab

defaultfactorymagicnumbersremovestrings

Some of Matlab's toolbox functions are affected by magic strings or magic numbers, which are strings or numbers with a deeper meaning besides the normal value. Both are considered as bad programming patters, because they provoke confusions, when the magic keys appear with the normal meaning by accident. See http://en.wikipedia.org/wiki/Anti-pattern
Example 1:
clear('myVariable')
clear('variables')
While the 1st clears the variable myVariable, the later clears all variables. Here 'variables' has a meta-meaning. The problem appears, when 'variables' is an existing variable:
a = 1;
variables = 2;
clear('variables')
disp(a) % >> 1
Only variables is cleared, which cannot be understood directly when its definition is 1000 lines before.
Example 2:
uicontrol('String', 'default')
This creates a button with the empty string '' instead of the expected 'default', because this is the magic string to invoke the default value get(0, 'DefaultUIControlString'). The same concerns properties of other graphic objects also, e.g. the 'name' property of figure or the string of uimenu. There is a workaround which allows the user to display 'default': Simply use '\default'. Unfortunately this is doubled magic, because in consequence it is impossible to display the string '\default'. Obviously a bad idea.
Example 3:
Graphic handles are doubles (although gobject of the new R2013a seems, like this is subject to changes? [EDITED: Yes, it changed with HG2 in R2014a]). But then a handle can be confused with data:
a = axes; % e.g. 0.0048828125
plot(a, 2, '+')
But you cannot draw the point [0.0048828125, 2] by this way, because the 1st input is considered as handle of the parent. Here all possible values of handles are magic. Collisions are very unlikely, but there is no way to avoid them reliably – as long as handles have the type double.
Question:
Which functions are concerned by magic values? What are the pitfalls and workarounds?

Best Answer

uicontrol's, figure's and uimenu's use these magic strings for properties, which are strings:
'default', 'remove', 'factory'
Such properties are e.g. 'String', 'Callback', 'Name', 'Tag', etc.
A bullet-proof workaround:
S = input('Input the button string: ', 's');
if any(strcmp(S, {'default', 'remove', 'factory'})
S = ['\', S];
elseif any(strcmp(S, {'\default', '\remove', '\factory'})
warning('String "%s" padded to support uicontrol!', S);
S = [' ', S];
end
uicontrol('String', S);
Such an ugly catching of rare exceptions is a typical effect of magic strings.
When a callback function is called 'default', use the function handle instead of a string:
uicontrol('Callback', @default);
A real solution, not only a workaround, is to strictly avoid using these names as function names or as 'String' property and never allow not controllable user-input as a property. But this can be a limitation, if e.g. the ten most frequent words of an arbitrary text file should be displayed in 10 buttons.