23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Variables<br />

Initializing Persistent Variables. When you declare a persistent variable,<br />

<strong>MATLAB</strong> initializes its value to an empty matrix, []. After the declaration<br />

statement, you can assign your own value to it. This is often done using an<br />

isempty statement, as shown here:<br />

function findSum(inputvalue)<br />

persistent SUM_X<br />

if isempty(SUM_X)<br />

SUM_X = 0;<br />

end<br />

SUM_X = SUM_X + inputvalue<br />

This initializes the variable to 0 the first time you execute the function, and<br />

then it accumulates the valueoneachiteration.<br />

Naming Variables<br />

<strong>MATLAB</strong> variable names must begin with a letter, which may be followed by<br />

any combination of letters, digits, and underscores. <strong>MATLAB</strong> distinguishes<br />

between uppercase and lowercase characters, so A and a arenotthesame<br />

variable.<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.<br />

N = namelengthmax<br />

N =<br />

63<br />

The genvarname function can be useful in creating variable names that are<br />

both valid and unique. See the genvarname reference page to find out how to<br />

do this.<br />

3-7

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

Saved successfully!

Ooh no, something went wrong!