MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

Raviteja
7

Good programming practice...

Raviteja
さんによって質問されました 2011 年 9 月 22 日
最新アクティビティ Jitin Beri さんによって 編集されました 2018 年 11 月 12 日
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!

  2 件のコメント

Michael Leung 2011 年 9 月 22 日
You are right! i is undesirable for the loop variable as matlab treats i as the complex number, sqrt(-1).
I personalty use ii and jj for my loops, but as long as its not the lonesome "i" it will speed up your code.
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.

サインイン to comment.

16 件の回答

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.

  1 件のコメント

K E
2012 年 1 月 26 日
Elements of Matlab Style is great

サインイン to comment.


回答者: Jake Bolanski 2011 年 9 月 22 日

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.

  1 件のコメント

Jan
2011 年 9 月 22 日
I agree. If you want to share your code, or do this ever in the future, it is a good idea to use the standard comment style of Matlab:
function [Output, ...] = Fcn(Input, ...)
% H1 line
% Help text
% [Output, ...] = Fcn(Input, ...)
% ...
% Date, author

サインイン to comment.


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.

  2 件のコメント

Daniel Shub
2011 年 9 月 23 日
I wish I could vote many many times.
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!

サインイン to comment.


Fangjun Jiang
回答者: Fangjun Jiang
2011 年 9 月 22 日

Where do you want to start? I'll add as I come up with some.
  • pre-allocate large size variables using zeros(), cell()

  0 件のコメント

サインイン to comment.


Jan
回答者: Jan
2011 年 9 月 22 日

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!

  1 件のコメント

Jan
2012 年 1 月 26 日
@Steven: As you've found out, longer symbols do not need more time (as long as the lookup table is not poluted by EVALs). While runtime is not a problem, debug time is. "B=max(1:10)" is confusing.

サインイン to comment.


Jan
回答者: Jan
2011 年 9 月 22 日

Do not start a script or function by:
clear all
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.

  1 件のコメント

Daniel Shub
2011 年 10 月 20 日
Even the best sometimes do it:
http://blogs.mathworks.com/loren/2009/06/16/rooting-around-in-matlab-part-2/

サインイン to comment.


Daniel Shub
回答者: Daniel Shub
2011 年 9 月 23 日
編集済み: Jitin Beri 2018 年 11 月 12 日

  1 件のコメント

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.

サインイン to comment.


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')

  2 件のコメント

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
2011 年 9 月 23 日
@jan you are correct, catching the output of load is an important piece of good practice.

サインイン to comment.


Jan
回答者: Jan
2011 年 9 月 22 日

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.

  1 件のコメント

Walter Roberson
2011 年 9 月 23 日
This one I cannot really agree with. It trades the possibility that one might want to share a section of the code in Answers or CSSM, against the near certainty that the code will need to be easily understandable by other people in one's workplace.
There are also locations were writing the comments and help text in English would violate various language preservation laws.

サインイン to comment.


Jan
回答者: Jan
2011 年 9 月 22 日

Include a check of number and type of inputs if all functions.
This will cost several seconds runtime, but will save hours of debugging.

  0 件のコメント

サインイン to comment.


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.

  0 件のコメント

サインイン to comment.


Jan
回答者: Jan
2011 年 9 月 23 日

  0 件のコメント

サインイン to comment.


Daniel Shub
回答者: Daniel Shub
2011 年 10 月 20 日

  1 件のコメント

Fangjun Jiang
2011 年 10 月 20 日
+1. I remember reading Doug's post. That's a good one.

サインイン to comment.


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:

  0 件のコメント

サインイン to comment.


Raviteja
回答者: Raviteja
2011 年 9 月 23 日

I got good help from matlab datasheets for matlab programming tips
http://www.mathworks.in/help/pdf_doc/matlab/programming_tips.pdf

  10 件のコメント

Walter Roberson
2011 年 9 月 23 日
@Mohammad: http://www.flickr.com/photos/gothamoddisee/216748915/
(Be sure to read the caption)
mohammad 2011 年 9 月 23 日
Nice, thanks Walter
mohammad 2011 年 9 月 24 日
I use this: www.google.com/bookmarks
but it's to be nice if here has itself Bookmarks

サインイン to comment.


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.

  1 件のコメント

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.

サインイン to comment.



Translated by