tharwan.de

Some Words About Physics, Python And The World In General

MATLAB – The Language

MATLAB is not only a tool it although describes a programming language. One that as outgrown itself years ago. MATLAB was designed as a MATrix LABoratory but the language is now used for general purpose. There are a lot of little and some bigger things where you have the feeling you do something that this is not made for when you program with MATLAB. Some Examples follow – more will come in later posts.

Variable Unpacking:

A function can return multiple matrices and so you would guess you can do some kind for variable unpacking. You actually can, but I have know idea why the bothered to implemented it anyway as limited as it ended up being.
We assume our function f(x) returns three variables a,b and c:

function [a,b,c]=f(x)
    % some clever math
    a = ;
    b = ;
    c = ;
end

Now when you do:

X = f(x);

All you get is a. To get a,b and c you have to do:

[a,b,c] = f(x);

Lets assume you are only interested in one or two of the return values you can do:

[~,b,c] = f(x) % works

Since you can substitute the commas in MATLAB with spaces this also works:

[a b ~] = f(x) % works

But not this:

[~ b c] = f(x) % does not work

You can not do something like:

l = [1 2 3];
[a,b,c] = l; % ERROR

Cellarrays will not help either:

l = {1 2 3};
[a,b,c] = l; % ERROR

Optional Arguments

This issue thing will hit very often while you are still develop your algorithm and the structure of your problem is not yet entirely clear. You can argue that this is my personal problem and I should think more before I start to program. Be assured I think a lot when I program but sometimes it works really well for me to just start writing down some code. But even if your program is completely laid out befor you start do write your code, almost certainly there will come a time you or someone else has to add something and here we are again.

Assume you have a function f(x,y) and later you discover there may be some circumstances where you need another argument z. You could refactor all your code and pass in three arguments for every call of f and pass in NaN for z where it was not needed before. Or you can use varargin:

function out = f(x,y,varargin)
    out = x+y
    if length(varargin)>0
        out = out * varargin{1}
    end

Honestly even the idea that you have a special argument you have to name varargin is kind of funny. It gets much more pretty if you have more than one optional argument and you have to think about some logic to figure out how many arguments you got, in what order and if they are all of the right type.

function out = f(x,y,varargin)
    out = x+y
    if length(varargin)==1
        out = out * varargin{1}
    elseif length(varargin)==2
        out = (out + varargin{2})*varargin{1}
    end

Matlab helps you with the inputParser. InputParser then does what the language should to by default. It gives you a way to test the arguments for type and set default values. It also gives you the opportunity to write much more code.

The way it would like to see this solved is like it is done in Python:

def f(x,y,z=1,v=0)
    return (x+y+v)*z

This even allows you to do:

f(1,2,v=1)

The only thing you may have may have to add is to check if all variables are of the right type, what is quite hard since you probably don‘t care about float or integer but the availability of + and * operators for the objects. No matter how elegant you code your varargin check, I doubt it will ever be as clear as Pythons syntax.

to be continued…