Alex Rivera | Logout

Good practices in writing MATLAB code?

Asked 2010-08-30T20:09:24.760
45

I would like to know the basic principles and etiquette of writing a well structured code.

Edit
Report

3 Answers

2

European Standards For Writing and Documenting Exchangeable Fortran 90 Code have been in my bookmarks, like forever. Also, there was a thread in here, since you are interested in MATLAB, on organising MATLAB code.

answered 2010-08-30T21:26:35.337
2

Personally, I've found that I learned more about programming style from working through SICP which is the MIT Intro to Comp SCI text (I'm about a quarter of the way through.) Than any other book. That being said, If you're going to be working in Python, the Google style guide is an excellent place to start.

I read somewhere that most programs (scripts anyways) should never be more than a couple of lines long. All the requisite functionality should be abstracted into functions or classes. I tend to agree.

answered 2010-08-30T21:33:29.490
2

A small addition to the wonderful answers already here regarding Matlab:

  • Avoid long scripts, instead write functions (sub routines) in separate files. This will make the code more readable and easier to optimize.

  • Use Matlab's built-in functions capabilities. That is, learn about the many many functions that Matlab offers instead of reinventing the wheel.

  • Use code sectioning, and whatever the other code structure the newest Matlab version offers.

  • Learn how to benchmark your code using timeit and profile . You'll discover that sometimes for loops are the better solution.

answered 2013-07-18T06:42:25.947

Your Answer