23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12 <strong>Programming</strong> Tips<br />

Variables<br />

This section covers the following topics:<br />

• “Rules for Variable Names” on page 12-26<br />

• “Making Sure Variable Names Are Valid” on page 12-26<br />

• “Don’t Use Function Names for Variables” on page 12-27<br />

• “Checking for Reserved Keywords” on page 12-27<br />

• “Avoid Using i and j for Variables” on page 12-28<br />

• “Avoid Overwriting Variables in Scripts” on page 12-28<br />

• “Persistent Variables” on page 12-28<br />

• “Protecting Persistent Variables” on page 12-28<br />

• “Global Variables” on page 12-29<br />

Rules for Variable Names<br />

Although variable names can be of any length, <strong>MATLAB</strong> uses only the first<br />

N characters of the name, (where N is the number returned by the function<br />

namelengthmax), and ignores the rest. Hence, it is important to make<br />

each variable name unique in the first N characters to enable <strong>MATLAB</strong> to<br />

distinguish variables. Also note that variable names are case sensitive.<br />

N = namelengthmax<br />

N =<br />

63<br />

For more information: See “Naming Variables” on page 3-7 in the <strong>MATLAB</strong><br />

<strong>Programming</strong> documentation.<br />

Making Sure Variable Names Are Valid<br />

Before using a new variable name, you can check to see if it is valid with the<br />

isvarname function. Note that isvarname does not consider names longer<br />

than namelengthmax characters to be valid.<br />

For example, the following name cannot be used for a variable since it begins<br />

with a number.<br />

12-26

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!