MATLAB: The “know it cold” list

Very often, the MATLAB documentation is enough for anybody with a vague idea of what they want to dig their way to the right concepts/features to solve their problem. Nonetheless, there are few handy tools or quirks that are “either you know it or you don’t”.

Most of the tips in the following list are essential to avoid super-clumsy maneuvers. I’ve seen enough people that developed messy workarounds just because they didn’t know these:

  • addpath( genpath() ): now you can organize your code by folder and add it in one shot! Preach it!
    addpath(…, ‘-end’): gives the new paths lower priority to avoid shadowing existing functions
  • userpath(‘reset’): fixes it when you see MATLAB starting in “C:\Windows\System32”. 

  • verLessThan(): deal with backward compatibility issues
  • struct2table(ver()) instead of plain ver(), which gives you a nasty array of structs
    struct2table(dir()) instead of plain dir(), which gives you a nasty array of structs

  • spotting implicit run-length encoding
  • spotting implicit uses of diff()/cumsum().
    diff() is easy to come up with, but we often missed cumsum().
  • comma separated list from dereferencing multiple cells (e..g C{:}) or multiple array-of-structs.
  • [varargout{1:nargout}] = f(…)
  • relay varargin by varargin{:}
  • use exist(…, ‘var’) to define simple optional arguments instead of counting argument numbers (nargin).

I’ll expand the list as I recall more concepts. Keep checking this page for updates. Feel free to suggest more in the comments section

77 total views, 2 views today