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

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

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

Section 4.6 Nested Functions <strong>in</strong> <strong>Matlab</strong> 415<br />

The cell <strong>of</strong> str<strong>in</strong>gs is stored <strong>in</strong> l<strong>in</strong>eStyleChoices and <strong>the</strong> number <strong>in</strong>dicat<strong>in</strong>g<br />

<strong>the</strong> choice is stored <strong>in</strong> l<strong>in</strong>eStyleChoice. We obta<strong>in</strong> <strong>the</strong> str<strong>in</strong>g describ<strong>in</strong>g<br />

<strong>the</strong> choice with cell <strong>in</strong>dex<strong>in</strong>g. Note <strong>the</strong> use <strong>of</strong> <strong>the</strong> curly braces.<br />

• A switch structure is <strong>the</strong>n used to set <strong>the</strong> l<strong>in</strong>e style based on <strong>the</strong> str<strong>in</strong>g describ<strong>in</strong>g<br />

<strong>the</strong> choice that is stored <strong>in</strong> l<strong>in</strong>eStyle. Because <strong>the</strong> callback is a nested<br />

function, it can access <strong>the</strong> handle hL<strong>in</strong>e <strong>of</strong> <strong>the</strong> plot, <strong>the</strong>n use it to set <strong>the</strong> l<strong>in</strong>e<br />

style accord<strong>in</strong>g to <strong>the</strong> choice <strong>in</strong> l<strong>in</strong>eStyle.<br />

At this po<strong>in</strong>t, you can run <strong>the</strong> GUI aga<strong>in</strong>. Select <strong>the</strong> ‘Red’ radio button and<br />

<strong>the</strong> ‘Dashed’ l<strong>in</strong>estyle from <strong>the</strong> popup menu. The result is shown <strong>in</strong> Figure 4.16.<br />

Figure 4.16.<br />

Dashed and red.<br />

Edit Boxes<br />

We will add one last uicontrol to our GUI, an edit box. An edit box provides<br />

an area where a user can enter text. Unlike static text, however, an edit box is<br />

dynamic. The user can change what is entered <strong>the</strong>re, and on <strong>the</strong> o<strong>the</strong>r hand, your<br />

program can also update <strong>the</strong> contents <strong>of</strong> an edit box.<br />

However, before we create an edit box on our GUI, we’ll first place some static<br />

text above <strong>the</strong> planned location for <strong>the</strong> edit box. The text will provide <strong>the</strong> user<br />

with <strong>in</strong>formation relevant for <strong>the</strong> edit box. Enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es just below<br />

<strong>the</strong> last uicontrol, <strong>the</strong> popup uicontrol for l<strong>in</strong>e styles. As static text controls<br />

were expla<strong>in</strong>ed above, and <strong>the</strong>re is noth<strong>in</strong>g new about this current entry o<strong>the</strong>r<br />

than <strong>the</strong> ‘Str<strong>in</strong>g’ value, we <strong>of</strong>fer <strong>the</strong> code without comment.

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

Saved successfully!

Ooh no, something went wrong!