views:

335

answers:

5

I'm getting some new students soon, who will be writing Matlab code. They're new to Matlab, but they have experience coding in Java and C++.

I'm going to have them go through the 'Getting Started' section of the Matlab help. In addition, I want to give a small tutorial with the goal to prevent them from making some of the most common mistakes people make when switching to Matlab ("Matlab starts counting at 1"), and show them some features that they may not be aware of when coming from other languages ("you can subtract a scalar directly from an array, and for vectors, there's bsxfun").

What are the most important things I should tell them?

+5  A: 

Enough snippy comments, here's something of an answer too:

  1. The Matlab desktop: what all the windows are for, dragging code from the history back into the command window, the variable inspector, etc.
  2. Plotting: not just the plot command, but how to use the plot GUI tools, and how to create an M-file from a graphic.
  3. M-files for scripts and functions, and the key differences between them.
  4. M-Lint, the profiler.
  5. Use Matlab as a vehicle for teaching the perils and pitfalls of floating-point arithmetic.
  6. Getting help: at the command line, on the web, documentation, the file exchange, ...
  7. Set path and the current working directory.
  8. Importing data from files, exporting data to files, loading and saving.

That should be enough to keep them busy for an hour or so.

To clarify, I propose these topics to help you teach your students to avoid common Matlab errors including;

  1. Unproductive use of the tool, retyping commands which can easily be recalled from the history, using C (or Java) style file reading commands instead of uuimport, slowly typing scripts to draw graphics when Matlab can do it for you, wondering what all the little orange lines in the editor right margin mean and the squiggly underlines, trying to figure things out for themselves when the help facilities could tell them, tons of other stuff that many much more experience Matlab users have taken ages to learn.
  2. Floating point arithmetic is not real.
  3. and probably a lot of other stuff too.
High Performance Mark
Being able to read the help properly is probably the most useful thing I'll teach them (other than getting an account on SO).
Jonas
What do you mean with 'floating point arithmetic is not real'?
Jonas
@Jonas: all I mean is that floating-point arithmetic does not exactly implement arithmetic on real numbers. Regular monitoring of SO shows that there are a lot of programmers, including experienced ones, who take a while to 'get' this.
High Performance Mark
@Jonas: comp.soft-sys.matlab is, dare I say it, even better than SO for Matlab specific questions and answers.
High Performance Mark
@High Performance Mark: Ah, I see what you mean. Yes, that may be relevant if suddenly `0` is not equal to `0`. I personally prefer SO over comp.soft-sys.matlab because of the higher SNR and, you know, my students may get a question about their SO rep on a job interview eventually :)
Jonas
nice post; wish I had such an introduction to MATLAB... What we had was one or two lectures that sort of featured MATLAB code, then we were given assignment to solve on MATLAB. Sort of like being thrown in to the deep-end of the pool and hoping for the best... :)
posdef
+5  A: 

With respect to unexpected or non-intuitive MATLAB features that may cause them confusion, there are some good pointers in this question:

With respect to cool time-saving/efficiency tricks, this other question has some nice examples:

And for a few potentially more advanced topics, you can refer to the answers to this question:

Now for my $0.02. Based on the sorts of questions I've seen asked most frequently on SO, I'd say you will want to make sure they have a good understanding of the following concepts:

And here are some neat features that are already implemented in MATLAB that may save them some time and effort:

gnovice
Yes, I should definitely mention `kron`. Nothing can't be solved by that function :)
Jonas
+5  A: 

I agree with previous answers, but I'd say indexing is the first and the most important and complex concept in studying MATLAB. I saw many C programmers starting with MATLAB just write loops, a lot of loops, something ridiculous like

for i=1:10
    a(i)=i;
end

instead of simple a=1:10;.

So I'd suggest them to read about matrix programming concepts:

  • How to create simple vectors and matrices
  • Which variables can be used for indexing
  • How to create and apply indexes
  • Logical operations and functions, logical and numeric indexes (find function)
  • Indexing right and left side of expression
  • Difference between indexing numerical matrices and cell arrays
  • How to use indexes as output from different functions, like sort, unique, ismember, etc.
  • You cannot apply indexes to intermediate results

As for productivity, I would add that knowing how to use editor's cell mode is very useful.

yuk
Indexing is the second part of the tutorial (after the counting thing). I will make sure I touch on all your points.
Jonas
+3  A: 

MATLAB is conceptually in some ways very different from other languages you mentioned:

  • cells are used were Java uses upcasting
  • global and persistent variables are static in Java
  • gui handles being just numbers of type double
  • nested functions are closures, neither Java nor C/C++ has such feature
  • seldom used private and @TYPE folders for visibility scoping
  • array handling tricks
  • very easy interoperability with Java/COM/.Net using MATLAB syntax
  • variadic function arguments, handling of function arguments with varargin / varargout
  • memory management
Mikhail
I won't tell them about global and persistent variables in the hope that I'll never see them use them :). Lots of good points, though!
Jonas
globals, no - persistents however, are very useful. Like statics, they provide a well-localized persistent data store that otherwise might have to be exposed, or use globals.
Marc
+3  A: 

For those coming from C-family languages, the element-wise operators are new. It took me a couple of months to discover the ./ and .* operators. Before that, I used to write for loops for element-wise operations. So perhaps that's something that should be pointed out.

Chinmay Kanchi
That's exactly the kind of answers I was looking for!. I thought I'd mention the element-wise operators in passing, now I'll make a bigger fuss about it.
Jonas