MATLAB Programming
MATLAB Programming MATLAB Programming
12 Programming Tips AvoidUsingiandjforVariables MATLAB uses the characters i and j to represent imaginary units. Avoid using i and j for variable names if you intend to use them in complex arithmetic. If you want to create a complex number without using i and j, youcanuse the complex function. Avoid Overwriting Variables in Scripts MATLAB scripts store their variables in a workspace that is shared with the caller of the script. When called from the command line, they share the base workspace. When called from a function, they share that function’s workspace. If you run a script that alters a variable that already exists in the caller’s workspace, that variable is overwritten by the script. For more information: See “M-File Scripts” on page 4-18 in the MATLAB Programming documentation. Persistent Variables To get the equivalent of a static variable in MATLAB, use persistent. When you declare a variable to be persistent within a function, its value is retained in memory between calls to that function. Unlike global variables, persistent variables are known only to the function in which they are declared. For more information: See “Persistent Variables” on page 3-6 in the MATLAB Programming documentation. Protecting Persistent Variables You can inadvertently clear persistent variables from memory by either modifying the function in which the variables are defined, or by clearing the function with one of the following commands: clear all clear functions 12-28
Variables Locking the M-file in memory with mlock prevents any persistent variables defined in the file from being reinitialized. Global Variables Use global variables sparingly. The global workspace is shared by all of your functions and also by your interactive MATLAB session. The more global variables you use, the greater the chances of unintentionally reusing a variable name, thus leaving yourself open to having those variables change in valueunexpectedly.Thiscanbeadifficultbugtotrackdown. For more information: See “Global Variables” on page 3-4 in the MATLAB Programming documentation. 12-29
- 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 796 and 797: 12 Programming Tips Commenting Out
- 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 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
- Page 846 and 847: Index debugging 9-6 designing 9-9 j
- Page 848 and 849: Index string, vector of input 2-70
- Page 850 and 851: Index internet 6-116 downloading fr
- Page 852 and 853: Index overloaded 5-37 function work
- Page 854 and 855: Index I if and empty arrays 3-89 ex
- Page 856 and 857: Index scalar 1-47 See also matrices
- Page 858 and 859: Index inheritance multiple 9-40 sim
- Page 860 and 861: Index reading HDF4 data 7-52 from t
12 <strong>Programming</strong> Tips<br />
AvoidUsingiandjforVariables<br />
<strong>MATLAB</strong> uses the characters i and j to represent imaginary units. Avoid<br />
using i and j for variable names if you intend to use them in complex<br />
arithmetic.<br />
If you want to create a complex number without using i and j, youcanuse<br />
the complex function.<br />
Avoid Overwriting Variables in Scripts<br />
<strong>MATLAB</strong> scripts store their variables in a workspace that is shared with<br />
the caller of the script. When called from the command line, they share the<br />
base workspace. When called from a function, they share that function’s<br />
workspace. If you run a script that alters a variable that already exists in the<br />
caller’s workspace, that variable is overwritten by the script.<br />
For more information: See “M-File Scripts” on page 4-18 in the <strong>MATLAB</strong><br />
<strong>Programming</strong> documentation.<br />
Persistent Variables<br />
To get the equivalent of a static variable in <strong>MATLAB</strong>, use persistent.<br />
When you declare a variable to be persistent within a function, its value is<br />
retained in memory between calls to that function. Unlike global variables,<br />
persistent variables are known only to the function in which they are<br />
declared.<br />
For more information: See “Persistent Variables” on page 3-6 in the<br />
<strong>MATLAB</strong> <strong>Programming</strong> documentation.<br />
Protecting Persistent Variables<br />
You can inadvertently clear persistent variables from memory by either<br />
modifying the function in which the variables are defined, or by clearing the<br />
function with one of the following commands:<br />
clear all<br />
clear functions<br />
12-28