MATLAB Programming
MATLAB Programming MATLAB Programming
12 Programming Tips Commenting Out a Block of Code To comment out a block of text or code within the MATLAB editor, 1 Highlight the block of text you would like to comment out. 2 Holding the mouse over the highlighted text, select Text > Comment (or Uncomment, to do the reverse) from the toolbar. (You can also get these options by right-clicking the mouse.) For more information: See Commenting in the MATLAB Desktop Tools and Development Environment documentation. Creating M-Files from Command History If there is part of your current MATLAB session that you would like to put into an M-file, this is easily done using the Command History window: 1 Open this window by selecting View > Command History. 2 Use Shift+Click or Ctrl+Click to select the lines you want to use. MATLAB highlights the selected lines. 3 Right-click once, and select Create M-File from the menu that appears. MATLAB creates a new Editor window displaying the selected code. Editing M-Files in EMACS If you use Emacs, you can download editing modes for editing M-files with GNU-Emacs or with early versions of Emacs from the MATLAB Central Web site: http://www.mathworks.com/matlabcentral/ At this Web site, select File Exchange, andthenUtilities > Emacs. For more information: See General Preferences for the Editor/Debugger in the MATLAB Desktop Tools and Development Environment documentation. 12-12
M-File Functions M-File Functions This section covers the following topics: • “M-File Structure” on page 12-13 • “Using Lowercase for Function Names” on page 12-13 • “Getting a Function’s Name and Path” on page 12-14 • “What M-Files Does a Function Use?” on page 12-14 • “Dependent Functions, Built-Ins, Classes” on page 12-15 M-File Structure An M-File consists of the components shown here: function [x, y] = myfun(a, b, c) % Function definition line % H1 line -- A one-line summary of the function's purpose. % Help text -- One or more lines of help text that explain % how to use the function. This text is displayed when % the user types "help functionname". % The Function body normally starts after the first blank line. % Comments -- Description (for internal use) of what the % function does, what inputs are expected, what outputs % are generated. Typing "help functionname" does not display % this text. x = prod(a, b); % Start of Function code For more information: See “Basic Parts of an M-File” on page 4-9 of the MATLAB Programming documentation. Using Lowercase for Function Names Function names appear in uppercase in MATLAB help text only to make the help easier to read. In practice, however, it is usually best to use lowercase when calling functions. 12-13
- Page 746 and 747: 10 Scheduling Program Execution wit
- Page 748 and 749: 10 Scheduling Program Execution wit
- Page 750 and 751: 10 Scheduling Program Execution wit
- Page 752 and 753: 10 Scheduling Program Execution wit
- Page 754 and 755: 10 Scheduling Program Execution wit
- Page 756 and 757: 10 Scheduling Program Execution wit
- Page 758 and 759: 10 Scheduling Program Execution wit
- Page 760 and 761: 10 Scheduling Program Execution wit
- Page 762 and 763: 11 Improving Performance and Memory
- Page 764 and 765: 11 Improving Performance and Memory
- Page 766 and 767: 11 Improving Performance and Memory
- Page 768 and 769: 11 Improving Performance and Memory
- Page 770 and 771: 11 Improving Performance and Memory
- Page 772 and 773: 11 Improving Performance and Memory
- Page 774 and 775: 11 Improving Performance and Memory
- Page 776 and 777: 11 Improving Performance and Memory
- Page 778 and 779: 11 Improving Performance and Memory
- Page 780 and 781: 11 Improving Performance and Memory
- Page 782 and 783: 11 Improving Performance and Memory
- Page 784 and 785: 11 Improving Performance and Memory
- Page 786 and 787: 12 Programming Tips MATLAB Path (p.
- Page 788 and 789: 12 Programming Tips numberOfLines)
- Page 790 and 791: 12 Programming Tips Help This secti
- Page 792 and 793: 12 Programming Tips Topic Name arit
- Page 794 and 795: 12 Programming Tips Youcangethelpon
- Page 798 and 799: 12 Programming Tips For M-file func
- Page 800 and 801: 12 Programming Tips Function Argume
- Page 802 and 803: 12 Programming Tips Passing Argumen
- Page 804 and 805: 12 Programming Tips For more inform
- Page 806 and 807: 12 Programming Tips Debugging This
- Page 808 and 809: 12 Programming Tips Setting Breakpo
- Page 810 and 811: 12 Programming Tips Variables This
- Page 812 and 813: 12 Programming Tips AvoidUsingiandj
- Page 814 and 815: 12 Programming Tips Strings This se
- Page 816 and 817: 12 Programming Tips Function regexp
- Page 818 and 819: 12 Programming Tips are useful when
- Page 820 and 821: 12 Programming Tips File Precedence
- Page 822 and 823: 12 Programming Tips Making Nontoolb
- Page 824 and 825: 12 Programming Tips Using switch Ve
- Page 826 and 827: 12 Programming Tips try X = A * B c
- Page 828 and 829: 12 Programming Tips • Load spread
- Page 830 and 831: 12 Programming Tips Files and Filen
- Page 832 and 833: 12 Programming Tips -- METHOD #1 --
- Page 834 and 835: 12 Programming Tips Readable File F
- Page 836 and 837: 12 Programming Tips Starting MATLAB
- Page 838 and 839: 12 Programming Tips Finding the MAT
- Page 840 and 841: 12 Programming Tips For More Inform
- Page 842 and 843: A External Interfaces A-2
- Page 844 and 845: Index arithmetic operators 3-17 ove
12 <strong>Programming</strong> Tips<br />
Commenting Out a Block of Code<br />
To comment out a block of text or code within the <strong>MATLAB</strong> editor,<br />
1 Highlight the block of text you would like to comment out.<br />
2 Holding the mouse over the highlighted text, select Text > Comment (or<br />
Uncomment, to do the reverse) from the toolbar. (You can also get these<br />
options by right-clicking the mouse.)<br />
For more information: See Commenting in the <strong>MATLAB</strong> Desktop Tools<br />
and Development Environment documentation.<br />
Creating M-Files from Command History<br />
If there is part of your current <strong>MATLAB</strong> session that you would like to put<br />
into an M-file, this is easily done using the Command History window:<br />
1 Open this window by selecting View > Command History.<br />
2 Use Shift+Click or Ctrl+Click to select the lines you want to use.<br />
<strong>MATLAB</strong> highlights the selected lines.<br />
3 Right-click once, and select Create M-File from the menu that appears.<br />
<strong>MATLAB</strong> creates a new Editor window displaying the selected code.<br />
Editing M-Files in EMACS<br />
If you use Emacs, you can download editing modes for editing M-files with<br />
GNU-Emacs or with early versions of Emacs from the <strong>MATLAB</strong> Central Web<br />
site:<br />
http://www.mathworks.com/matlabcentral/<br />
At this Web site, select File Exchange, andthenUtilities > Emacs.<br />
For more information: See General Preferences for the Editor/Debugger in<br />
the <strong>MATLAB</strong> Desktop Tools and Development Environment documentation.<br />
12-12