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 />

Variables<br />

A <strong>MATLAB</strong> variable is essentially a tag that you assign to a value while that<br />

value remains in memory. The tag gives you a way to reference the value in<br />

memory so that your programs can read it, operate on it with other data, and<br />

save it back to memory. This section covers the following topics on using<br />

variables in <strong>MATLAB</strong>:<br />

• “Types of Variables” on page 3-3<br />

• “Naming Variables” on page 3-7<br />

• “Guidelines to Using Variables” on page 3-11<br />

• “Scope of a Variable” on page 3-11<br />

• “Lifetime of a Variable” on page 3-13<br />

Types of Variables<br />

<strong>MATLAB</strong> provides three basic types of variables:<br />

• “Local Variables” on page 3-3<br />

• “Global Variables” on page 3-4<br />

• “Persistent Variables” on page 3-6<br />

Local Variables<br />

Each <strong>MATLAB</strong> function has its own local variables. These are separate from<br />

those of other functions (except for nested functions), and from those of the<br />

base workspace. Variables defined in a function do not remain in memory from<br />

one function call to the next, unless they are defined as global or persistent.<br />

Scripts, on the other hand, do not have a separate workspace. They store their<br />

variables in a workspace that is shared with the caller of the script. When<br />

called from the command line, they share the base workspace. When called<br />

from a function, they share that function’s workspace.<br />

3-3

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

Saved successfully!

Ooh no, something went wrong!