Good programming practice...


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 2012년 1월 27일
It is only fair that if I plug Doug's blog, I should plug Loren's blog also:
Daniel Shub
Daniel Shub 2011년 10월 20일
Fangjun Jiang
Fangjun Jiang 2011년 10월 20일
+1. I remember reading Doug's post. That's a good one.
Steven
Steven 2011년 10월 19일
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 2012년 1월 27일
@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 2011년 10월 19일
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 2011년 9월 23일
Jan
Jan 2011년 9월 23일
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 2011년 9월 23일
"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 2011년 9월 23일
I wish I could vote many many times.
Jan
Jan 2011년 9월 23일
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 2011년 9월 23일
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 2011년 9월 23일
@jan you are correct, catching the output of load is an important piece of good practice.
Jan
Jan 2011년 9월 23일
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 2011년 9월 23일
Jan
Jan 2011년 9월 23일
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 2011년 9월 23일
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 2012년 1월 26일
Elements of Matlab Style is great