Note that there are some explanatory texts on larger screens.

plurals
  1. POCorner Cases, Unexpected and Unusual MATLAB
    text
    copied!<p>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.</p> <hr> <p>MATLAB <strong>always</strong> returns first output argument of a function (if it has at least one) into its caller workspace, also unexpectedly if function is being called without returning arguments like <code>myFunc1(); myFunc2();</code> the caller workspace still would contain first output of <code>myFunc2();</code> as "invisible" <code>ans</code> variable. It could play an important role if <code>ans</code> is a reference object - it would remain alive.</p> <hr> <pre><code>set([], 'Background:Color','red') </code></pre> <p>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 <code>harray = findobj(0,'Tag','NotExistingTag')</code></p> <hr> <pre><code>myArray([1,round(end/2)]) </code></pre> <p>This use of <code>end</code> keyword may seem unclean but is sometimes very handy instead of using <code>length(myArray)</code>.</p> <hr> <pre><code>any([]) ~= all([]) </code></pre> <p>Surprisigly <code>any([])</code> returns <code>false</code> and <code>all([])</code> returns <code>true</code>. And I always thought that <code>all</code> is stronger then <code>any</code>.</p> <p><strong>EDIT:</strong></p> <p>with <em>not empty</em> argument <code>all()</code> returns <code>true</code> for a subset of values for which <code>any()</code> returns <code>true</code> (e.g. truth table). This means that <code>any()</code> <code>false</code> implies <code>all()</code> <code>false</code>. This simple rule is being violated by MATLAB with <code>[]</code> as argument.</p> <p>Loren also <a href="http://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/" rel="noreferrer">blogged about it</a>.</p> <hr> <pre><code>Select(Range(ExcelComObj)) </code></pre> <p>Procedural style COM object method dispatch. Do not wonder that <code>exist('Select')</code> returns zero!</p> <hr> <pre><code>[myString, myCell] </code></pre> <p>MATLAB makes in this case an implicit cast of string variable <code>myString</code> to cell type <code>{myString}</code>. It works, also if I would not expect it to do so.</p> <hr> <pre><code>[double(1.8), uint8(123)] =&gt; 2 123 </code></pre> <p>Another cast example. Everybody would probably expect <code>uint8</code> value being cast to <code>double</code> but Mathworks have another opinion. Without a warning this behavior is very dangerous.</p> <hr> <pre><code>a = 5; b = a(); </code></pre> <p>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.</p> <hr> <p>Syntax <code>Foo(:)</code> works not only on data but also with functions if called as <code>Bar.Foo(:)</code>, in this scenario the function input argument is passed as char <em>colon</em> <code>':'</code>. </p> <p>For example let <code>Bar.Foo = @(x) disp(x)</code> Now calling <code>Bar.Foo(:)</code> prints char <code>':'</code> in the MATLAB Command Window.</p> <p>This strange feature works with all MATLAB 7 versions without warnings.</p> <hr> <pre><code>a = {'aa', 'bb' 'cc', 'dd'}; </code></pre> <p>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.</p> <p><strong>EDIT:</strong> very handy feature, see the comment by gnovice.</p> <hr> <pre><code>set(hobj, {'BackgroundColor','ForegroundColor'},{'red','blue'}) </code></pre> <p>This code does what you probably expect it to do. That function <code>set</code> accepts a struct as its second argument is a known fact and makes sense, and this sintax is just a <code>cell2struct</code> away.</p> <hr> <p>Equvalence rules are sometimes unexpected at first. For example <code>'A'==65</code> returns true (although for C-experts it is self-evident). Similarly <code>isequal([],{})</code> retuns, as expected, <code>false</code> and <code>isequal([],'')</code> returns <code>true</code>.</p> <p>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:</p> <pre><code>ind = strfind( [1 2 3 4 1 2 3 4 1 2 3 4 ], [2 3] ) </code></pre> <hr> <p>MATLAB function <code>isnumeric()</code> returns <code>false</code> for booleans. This feels just ... false :-)</p> <hr> <p>About which further unexpected/unusual MATLAB features are you aware?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload