15.11.2014 Views

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

368 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />

function localvar, <strong>the</strong>n returned it <strong>in</strong> <strong>the</strong> output variable. Because we assigned<br />

<strong>the</strong> output <strong>of</strong> <strong>the</strong> function localvar to <strong>the</strong> base workspace variable p with <strong>the</strong><br />

command p=localvar(p), <strong>the</strong> value <strong>of</strong> p <strong>in</strong> <strong>the</strong> base workspace changed. Note<br />

that <strong>the</strong> command localvar(p) would have no effect on <strong>the</strong> workspace variable<br />

p. It’s <strong>the</strong> fact that we assign <strong>the</strong> output that causes <strong>the</strong> change to <strong>the</strong> base<br />

workspace variable p.<br />

Global Variables<br />

One way to allow a function access to variables <strong>in</strong> a caller’s workspace is to declare<br />

<strong>the</strong>m as global variables. You must do this <strong>in</strong> <strong>the</strong> caller’s workspace and <strong>in</strong> <strong>the</strong><br />

function that wants access to <strong>the</strong>se variables. As an example, open <strong>the</strong> editor,<br />

enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es, and save <strong>the</strong> function M-file as localvar.m.<br />

function localvar<br />

global P<br />

fpr<strong>in</strong>tf(’In localvar, <strong>the</strong> value <strong>of</strong> P is: %d\n’, P)<br />

Note that this adaption <strong>of</strong> localvar receives no <strong>in</strong>put nor does it output anyth<strong>in</strong>g.<br />

However, it does declare P as a global variable. Consequently, if <strong>the</strong> caller does<br />

<strong>the</strong> same <strong>in</strong> its workspace, <strong>the</strong> function localvar should be able to access <strong>the</strong><br />

global variable P .<br />

In <strong>the</strong> command w<strong>in</strong>dow, we’ll assign a value to P . Global variables should<br />

first be declared before <strong>the</strong>y are assigned a value, after which we assign P a value<br />

<strong>of</strong> 50.<br />

>> global P<br />

>> P=50<br />

P =<br />

50<br />

Execute <strong>the</strong> function localvar by enter<strong>in</strong>g <strong>the</strong> command localvar at <strong>the</strong> <strong>Matlab</strong><br />

prompt <strong>in</strong> <strong>the</strong> command w<strong>in</strong>dow.<br />

>> localvar<br />

In localvar, <strong>the</strong> value <strong>of</strong> P is: 50<br />

This is a clear <strong>in</strong>dication that <strong>the</strong> variable P is shared by <strong>the</strong> base workspace and<br />

<strong>the</strong> workspace <strong>of</strong> <strong>the</strong> function localvar.

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

Saved successfully!

Ooh no, something went wrong!