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.

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

>> whos<br />

Name Size Bytes Class<br />

M 3x3 72 double array<br />

Scripts and <strong>the</strong> Base Workspace<br />

When you execute a script file from <strong>the</strong> <strong>Matlab</strong> prompt <strong>in</strong> <strong>the</strong> command w<strong>in</strong>dow,<br />

<strong>the</strong> script uses <strong>the</strong> base workspace. It can access any variables that are present<br />

<strong>in</strong> <strong>the</strong> base workspace, and any variables created by <strong>the</strong> script rema<strong>in</strong> <strong>in</strong> <strong>the</strong> base<br />

workspace when <strong>the</strong> script f<strong>in</strong>ishes. As an example, open <strong>the</strong> <strong>Matlab</strong> editor and<br />

enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es.<br />

x=1:5<br />

s=sum(x)<br />

p=prod(x)<br />

Save <strong>the</strong> script as sumprod.m and return to <strong>the</strong> <strong>Matlab</strong> command w<strong>in</strong>dow.<br />

Execute <strong>the</strong> script by typ<strong>in</strong>g its name at <strong>the</strong> command prompt. The script f<strong>in</strong>ds<br />

<strong>the</strong> sum and product <strong>of</strong> <strong>the</strong> <strong>in</strong>tegers from 1 to 5.<br />

>> sumprod<br />

x =<br />

1 2 3 4 5<br />

s =<br />

15<br />

p =<br />

120<br />

Exam<strong>in</strong>e <strong>the</strong> current state <strong>of</strong> <strong>the</strong> base workspace by typ<strong>in</strong>g <strong>the</strong> whos command<br />

at <strong>the</strong> command prompt.

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

Saved successfully!

Ooh no, something went wrong!