Over the years, reading others code, I encountered and collected some examples of MATLAB syntax which can be at first unusual and counterintuitive. Please, feel free to comment or complement this list. I verified it with r2006a.
set([], 'Background:Color','red')
MATLAB is very forgiving sometimes. In this case, setting properties to an array of objects works also with nonsense properties, at least when the array is empty. Such arrays usually come from harray = findobj(0,'Tag','NotExistingTag')
myArray([1,round(end/2)])
This use of end
keyword may seem unclean but is sometimes very handy instead of using length(myArray)
.
any([]) ~= all([])
Surprisigly any([])
returns false
and all([])
returns true
. And I always thought that all
is stronger then any
.
EDIT:
with not empty argument all()
returns true
for a subset of values for which any()
returns true
(e.g. truth table). This means that any()
false
implies all()
false
. This simple rule is being violated by MATLAB with []
as argument.
Loren also blogged about it.
Select(Range(ExcelComObj))
Procedural style COM object method dispatch. Do not wonder that exist('Select')
returns zero!
[myString, myCell]
MATLAB makes in this case an implicit cast of string variable myString
to cell type {myString}
. It works, also if I would not expect it to do so.
[double(1.8), uint8(123)] => 2 123
Another cast example. Everybody would probably expect uint8
value being cast to double
but Mathworks have another opinion. Without a warning this behavior is very dangerous.
a = 5;
b = a();
It looks silly but you can call a variable with round brackets. Actually it makes sense because this way you can execute a function given its handle.
Syntax Foo(:)
works not only on data but also with functions if called as Bar.Foo(:)
, in this scenario the function input argument is passed as char colon ':'
.
For example let Bar.Foo = @(x) disp(x)
Now calling Bar.Foo(:)
prints char ':'
in the MATLAB Command Window.
This strange feature works with all MATLAB 7 versions without warnings.
a = {'aa', 'bb'
'cc', 'dd'};
Surprsisingly this code neither returns a vector nor rises an error but defins matrix, using just code layout. It is probably a relict from ancient times.
EDIT: very handy feature, see the comment by gnovice.
set(hobj, {'BackgroundColor','ForegroundColor'},{'red','blue'})
This code does what you probably expect it to do. That function set
accepts a struct as its second argument is a known fact and makes sense, and this sintax is just a cell2struct
away.
Equvalence rules are sometimes unexpected at first. For example 'A'==65
returns true (although for C-experts it is self-evident). Similarly isequal([],{})
retuns, as expected, false
and isequal([],'')
returns true
.
The string-numeric equivalence means that all string functions can be used also for numeric arrays, for example to find indices of a sub-array in a large array:
ind = strfind( [1 2 3 4 1 2 3 4 1 2 3 4 ], [2 3] )
MATLAB function isnumeric()
returns false
for booleans. This feels just ... false :-)
About which further unexpected/unusual MATLAB features are you aware?