There are many acceptable ways of punctuating Matlab code in a .m (dot-m) file. Some people ask Steve why he uses the punctuation that he does. Here is the explanation. Consider the following simple for-loop: mylist = [ 1 2 3 4 5 ]; for i=1:numel(mylist), % note the comma after the for statement mylist(i) = mylist(i) * 2; i, % note the comma after the i end; % note the semicolon after the 'end' In a file or in pasted text, this code fragment can also be punctuated: mylist = [ 1 2 3 4 5 ]; for i=1:numel(mylist) mylist(i) = mylist(i) * 2; i end Why does Steve generally write using the first approach? The reason is that the code will run the same way if it is reduced to a single line: mylist = [1 2 3 4 5]; for i=1:numel(mylist), mylist(i) = mylist(i) * 2; i, end; % this will run mylist = [1 2 3 4 5]; for i=1:numel(mylist) mylist(i) = mylist(i) * 2; i end % this will not run Usually this is not important, but it allows the formatting to be invariant if the code is collapsed onto a single line or written out. So Steve's punctuation approach is to
|
-Techniques and tricks > Matlab >