Corner Cases, Unexpected and Unusual Matlab
Posted
by Mikhail
on Stack Overflow
See other posts from Stack Overflow
or by Mikhail
Published on 2009-11-10T18:51:40Z
Indexed on
2010/03/17
13:01 UTC
Read the original article
Hit count: 387
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 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.
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.
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.
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.
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).
About which further unexpected/unusual Matlab features are you aware?
© Stack Overflow or respective owner