Please explain good MATLAB programming practice methods. It will help to the guys who are new to programming like me.
Previously I used
after following some suggestions from this answers pages I learnt to use
This is the good way to write programs.
Like this, as a professional programmer, please mention some good programming practice techniques.
It will useful to all!
No products are associated with this question.
I suggest developing a coding "style." Some good places to start are given in the FAQ:
I also would consider this book reviewed by Loren
although I should say I have not looked at it yet. It is on my list of things to do.
It's always good to comment in front of your code so that you won't have trouble remembering why you did what you did. It also helps others recognize your code and what you're doing.
Where do you want to start? I'll add as I come up with some.
Avoid overloading builtin functions. It happens very often, that a user creates a variable called "max" and is surprised that the MAX function does not work anymore:
max = 19; ... 100 lines later: B = max(1:10) ??? Index exceeds matrix dimensions.
A confusing but correct error message!
Do not start a script or function by:
This removes all loaded functions from the memory. Reloading them will need a lot of time such that a program can be 100 times slower in a not so unlikely worst case.
I admit that a clear or the equivalent clear variables can help to detect crude typos in names of a variable, if this variable is existing before the script. But this is a really rare case and if a function is used, MLint detects this much better.
But there is a frightening large number of codes starting with clear all. I assume this is recommended in some tutorials which have been written without deeper insight into MATLAB.
Come to Answers before using global or eval. There is almost always a better way to do it.
Use the functional form of load and save, and almost every other function except maybe help and doc
data = load('filename.mat')
Use English for comments and the help text. Although you might assume, that you will never share the code, even a discussion in this forum is much harder, if all variables and descriptions are in Italian.
Include a check of number and type of inputs if all functions.
This will cost several seconds runtime, but will save hours of debugging.
Read the tips for program development in the dokumentation:
And if you are on the way, read the rest of the documentation also, at least the "Getting Strarted" chapters.
This is a list of helpful links: http://www.ee.columbia.edu/~marios/matlab/matlab_tricks.html
E.g. this is exhaustive: http://www.ee.columbia.edu/~marios/matlab/MatlabStyle1p5.pdf
Surprised Doug didn't chime in with his blog post: http://blogs.mathworks.com/videos/2010/03/08/top-10-matlab-code-practices-that-make-me-cry/
It is only fair that if I plug Doug's blog, I should plug Loren's blog also:
I got good help from matlab datasheets for matlab programming tips
However, the difference seems to be indistinguishable considering a certain precision whatever the number of repetitions.
tic; for i1 = 1:100000 x = sin(i1); end toc Elapsed time is 0.003597 seconds.
tic; for i = 1:100000 x = sin(i); end toc Elapsed time is 0.003569 seconds.