This command provides a number of options for stopping execution of a function

This command provides a number of options for

This preview shows page 67 - 68 out of 68 pages.

command. This command provides a number of options for stopping execution of a function. A particularly useful option is: dbstop if error This stops any function causing an error. Then just run the Matlab function. Execution will stop at the point where the error occurs, and you will get the Matlab prompt back so that you can examine variables or step through execution from that point. The command dbstep allows you to step through execution one line at a time. You can continue execution with the dbcont . To exit debug mode, type dbquit . For more information, use help for the following topics: dbstop , dbclear , dbcont , dbstep , dbtype , dbup and dbquit . 12.3 Recommended programming style Programming style is a set of conventions that programmers follow to standardize their code to some degree and to make the overall program easier to read and to debug. This will also allow you to quickly understand what you did in your program when you look at it weeks or months from now. The style conventions are for the reader only, but you will become that reader one day. Some style requirements and style guidelines are presented below. These are recommendations, and some personal variations in style are acceptable, but you should not ignore them. It is important to organize your 64
Image of page 67
programs properly since it will improve the readability, make the debugging task easier and save time of the potential readers. 1. You should always comment difficult parts of the program! But ... do not explain the obvious. 2. Comments describing tricky parts of the code, assumptions, or design decisions are suggested to be placed above the part of the code you are attempting to document. Try to avoid big blocks of comments except for the description of the m-file header. 3. Indent a few spaces (preferably 2 or 3) before lines of the code and comments inside the control flow structures. The layout should reflect the program ’flow’. Here is an example: x = 0:0.1:500; for i=1:length(x) if x(i) > 0 s(i) = sqrt(x(i)); else s(i) = 0; end end 4. Avoid the use of magic numbers; use a constant variable instead. When you need to change the number, you will have to do it only once, rather than searching all over your code. An example: % A BAD code that uses % This is the way it SHOULD be % magic numbers r = rand(1,50); n = 50; % number of points for i = 1:50 r = rand(1,n); data(i) = i * r(i); data = (1:n) .* r; end y = sum(data)/50; avr = sum(data)/n; disp([’Number of points is 50.’]); disp([’Number of points is ’,int2str(n)]); 5. Avoid the use of more than one code statement per line in your script or function m-files. 6. No line of code should exceed 80 characters (it is a rare case when this is not possible). 7. Avoid declaring global variables. You will hardly ever encounter a circumstance under which you will really need them. Global variables can get you into trouble without your noticing it!
Image of page 68

You've reached the end of your free preview.

Want to read all 68 pages?

  • Fall '13
  • Ivan
  • matlab, .........

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture