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.

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

So, let’s def<strong>in</strong>ed <strong>the</strong> callback for <strong>the</strong> popup menu <strong>of</strong> l<strong>in</strong>e styles. Just below <strong>the</strong><br />

callback function colorSelection_callback, add <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es.<br />

function L<strong>in</strong>eStylePopup_callback(hObject,eventdata)<br />

l<strong>in</strong>eStyleChoices=get(hObject,’Str<strong>in</strong>g’);<br />

l<strong>in</strong>eStyleChoice=get(hObject,’Value’);<br />

l<strong>in</strong>eStyle=l<strong>in</strong>eStyleChoices{l<strong>in</strong>eStyleChoice};<br />

switch l<strong>in</strong>eStyle<br />

case ’Solid’<br />

set(hL<strong>in</strong>e,’L<strong>in</strong>eStyle’,’-’)<br />

case ’Dotted’<br />

set(hL<strong>in</strong>e,’L<strong>in</strong>eStyle’,’:’)<br />

case ’DashDot’<br />

set(hL<strong>in</strong>e,’L<strong>in</strong>eStyle’,’-.’)<br />

case ’Dashed’<br />

set(hL<strong>in</strong>e,’L<strong>in</strong>eStyle’,’--’)<br />

case ’None’<br />

set(hL<strong>in</strong>e,’L<strong>in</strong>eStyle’,’none’)<br />

end<br />

end % end L<strong>in</strong>eStylePopup_callback<br />

We will make several comments regard<strong>in</strong>g this callback.<br />

• The first l<strong>in</strong>e conta<strong>in</strong>s <strong>the</strong> function keyword and <strong>the</strong> name <strong>of</strong> <strong>the</strong> callback, <strong>in</strong><br />

this case L<strong>in</strong>eStylePopup_callback. The descriptive name <strong>of</strong> this callback<br />

is a rem<strong>in</strong>der <strong>of</strong> it is designed to provide. Note <strong>the</strong> oblibatory arguments,<br />

hObject and eventdata. The argument hObject will receive <strong>the</strong> handle <strong>of</strong><br />

<strong>the</strong> call<strong>in</strong>g object (<strong>in</strong> this case, <strong>the</strong> handle <strong>of</strong> <strong>the</strong> popup menu uicontrol). In<br />

this example, <strong>the</strong> argument eventdata is not used, but it is still required. As<br />

more evolutions <strong>of</strong> <strong>Matlab</strong> are updated, plans are <strong>in</strong> <strong>the</strong> works to make more<br />

use <strong>of</strong> <strong>the</strong> argument eventdata (as we did with our uibuttongroup, but for<br />

<strong>the</strong> meantime, it’s not used <strong>in</strong> this callback, but it is still required.<br />

• Because hObject is passed <strong>the</strong> handle to <strong>the</strong> popup uicontrol, we can use it<br />

to access two properties <strong>of</strong> that control, <strong>the</strong> ‘Str<strong>in</strong>g’ and ‘Value’ properties.<br />

i. The ‘Str<strong>in</strong>g’ property conta<strong>in</strong>s a cell <strong>of</strong> str<strong>in</strong>gs, each <strong>of</strong> which describe a<br />

l<strong>in</strong>e style.<br />

ii. The ‘Value’ property conta<strong>in</strong>s a number <strong>in</strong>dicat<strong>in</strong>g which entry on <strong>the</strong><br />

popup menu was selected by <strong>the</strong> user.

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

Saved successfully!

Ooh no, something went wrong!