Wednesday, November 12, 2008

Evil MATLAB Copy-On-Assign

Saying this kind of thing is really annoying:

item = items{i};
item.whatever = 'something';
items{i} = item; % <-- The evil requirement.

Maybe I just need to learn the right idioms, but this kind of thing taken to much more complicated software is just killing me right now.

3 comments:

  1. In MATLAB, almost everything is "by value" instead of "by reference". That is, if you say "a = b", and then change b, a will stay the same.

    In this particular example, you can just index in there directly:

    items{i}.whatever='something';

    ReplyDelete
  2. Understood. I was exaggerating the situation here to give a simple example. It wasn't so simple in the program I was working on.

    But I've been thinking more about it and realized that MATLAB's rules give a semi-functional environment. That is, in general functions have no side effects. Maybe obvious, but thinking of it from that perspective helped me. So, within a local context, MATLAB isn't functional, but in the larger scale, it is (sort of).

    I'm also ignoring globals for the moment, since globals are obviously evil, anyway.

    So, I might forgive MATLAB and learn to get more in the groove. Just that deeply nested data structures (which I happened to be working with) take some additional thought.

    ReplyDelete
  3. MATLAB, like any other language, has its fair share of quirks.

    I suspect it was designed to be mostly functional with strict evaluation for simplicity, especially for people who aren't "programmers".

    ReplyDelete