Good programming practice...

Raviteja on 22 Sep 2011
Latest activity Reply by Daniel Shub on 27 Jan 2012

Hello all,
Please explain good MATLAB programming practice methods. It will help to the guys who are new to programming like me.
Previously I used
for i=1:10
after following some suggestions from this answers pages I learnt to use
for i1=1:100
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!
Daniel Shub
Daniel Shub on 27 Jan 2012
It is only fair that if I plug Doug's blog, I should plug Loren's blog also:
Daniel Shub
Daniel Shub on 20 Oct 2011
Fangjun Jiang
Fangjun Jiang on 20 Oct 2011
+1. I remember reading Doug's post. That's a good one.
Steven
Steven on 19 Oct 2011
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.
Jan
Jan on 27 Jan 2012
@Steven: You have posted this detail 3 times in this thread. Please consider, that the runtime is not affected by the length of the name of the variable, because internally the variable is accessed by a memory pointer taken from a lookup-table. This lookup-table is created when the function is loaded the first time only.
Steven
Steven on 19 Oct 2011
However, the difference seems to be indistinguishable considering a certain precision.
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.
Jan
Jan on 23 Sep 2011
Jan
Jan on 23 Sep 2011
Read the FAQ
It is very likely, that the questions, which concern other users frequently, do concern you also. It is very efficient to profit from the mistakes of others, instead to implement them by your own.
Bjorn Gustavsson
Bjorn Gustavsson on 23 Sep 2011
"It is very efficient to profit from the mistakes of others, instead to implement them by your own." was one of the funniest (sadly fun, funily said) statements I've come across this week!
Daniel Shub
Daniel Shub on 23 Sep 2011
I wish I could vote many many times.
Jan
Jan on 23 Sep 2011
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.
Daniel Shub
Daniel Shub on 23 Sep 2011
Use the functional form of load and save, and almost every other function except maybe help and doc
instead of
load filename.mat
use
data = load('filename.mat')
Daniel Shub
Daniel Shub on 23 Sep 2011
@jan you are correct, catching the output of load is an important piece of good practice.
Jan
Jan on 23 Sep 2011
Same for SAVE.
And catch the output: Data = load(FileName), otherwise you could find unexpected variables in your workspace like "max", which will shadow existing functions to your surprise. See http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22299
Daniel Shub
Daniel Shub on 23 Sep 2011
Jan
Jan on 23 Sep 2011
I wish, that you do *not* come to Answers, but proceed to the above two links immediately... But I vote the helpful links +1.
Daniel Shub
Daniel Shub on 23 Sep 2011
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.
K E
K E on 26 Jan 2012
Elements of Matlab Style is great