Programming Guidelines for Matlab

This page contains a list of Matlab guidelines that can make the life of students easier for the homework assignments. Consider seriously to use them as they can reduce the amount of work significantly ;)

Figures

  • Never store figures by hand by clicking on *save as*. If you need to change something in the figure, you need to do everything again. For each figure you need to generate, you should have an own script that creates the figure if you execute it. Save the current figure with
   print('-dpng', '-r300', filename) % for png
print('-depsc2', filename) % for eps

or for PDFs you can use

   function saveFigAsPDF ( filename, fig )
   if nargin == 1, fig = gcf(); end
   w = 5;
   h = 5;
   set(fig.CurrentAxes, 'LooseInset', get(fig.CurrentAxes, 'TightInset'));
   set(fig, 'PaperPosition', [0 0 w h]);
   set(fig, 'PaperSize', [w h]);
   saveas(fig, [filename '.pdf'])
  • Since saving the figures takes time, a nice trick is to have a flag in your code, e.g. saveFigures, to save the figures that your code produces if set. This way you can still execute fast without saving the figures.
  • When submitting a plot for an assignment, the plot needs to be reasonable and it should also look nice (at least it should not hurt the eye). Always use a proper font size, correctly label your axes, zoom on interesting parts if necessary. For different curves in the same plot, use different line styles and colors.
  • An easy function for generating plots with mean and standard deviation is shadedErrorBar.
  • Do not put too many curves in the same plot!

Debugging

  • Debugging in Matlab is very easy. Use breakpoints to detect the errors. A very useful command is
   dbstop if error

which makes Matlab stop in debug mode whenever an error occurs. This allows you to look at the variables and see what went wrong.

  • In the begining of your scipts, you should clean up your variables by
   clear variables

Avoid clear all as it also deletes breakpoints.

  • dbstack can be very useful to see where you are on the stack. Navigate on the stack with dbup and dbdown. There is also a pop-up box that you can click for choosing and showing the stack. If you say you do not understand the program flow of a program we provide to you, just add a breakpoint at an arbitrary position that interests you and look at the stack
  • Sometimes breakpoints can be really annoying as they tend to disappear. In this case it might make sense to hardcode the breakpoint. The command for a hardcoded breakpoint is
   keyboard

Coding Conventions

  • Program your code as modular as possible. Never copy paste code, but implement reuseable parts in functions.
  • Use meaningful names for your variables, ideally consisting of several words in camelCase. Avoid giving the same name as ones of built-in functions (e.g. "mean").
  • Avoid functions with many (more than 4-5) input and output parameters. If you need so many, try to use structures.
  • Never use global variables! Its a sin and you will go to hell.

Code Efficiency

  • Try to avoid for loops whenever you can as they are very inefficient. A much faster alternative is "bsxfun".
  • If your code is too slow, run the profiler on it. It shows you exactly the parts of your code which can be improved.

Numerics

  • Try to avoid the direct inversion of a matrix with the "^-1" operator. The "^-1" is numerically very problematic. If the inverse is multiplied (left or right) with another matrix or vector, the "^-1" operator can be replaced by "\" or "/". These operators give a much more accurate result!

  

zum Seitenanfang