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.

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

hL<strong>in</strong>eWidthText=uicontrol(...<br />

’Style’,’text’,...<br />

’Parent’,hPanel,...<br />

’Position’, [10 180 180 20],...<br />

’Str<strong>in</strong>g’, ’Enter Desired L<strong>in</strong>eWidth: (1-10)’,...<br />

’HorizontalAlignment’,’Left’,...<br />

’BackgroundColor’, panel_color);<br />

Next, to <strong>in</strong>itialize <strong>the</strong> edit box for our GUI, 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> static text control with handle hL<strong>in</strong>eWidthText.<br />

hL<strong>in</strong>eWidthEditbox=uicontrol(...<br />

’Style’,’edit’,...<br />

’Parent’,hPanel,...<br />

’Units’,’Pixels’,...<br />

’Position’, [10 150 180 25],...<br />

’Str<strong>in</strong>g’,’1’,...<br />

’BackgroundColor’, buttongroup_color,...<br />

’Callback’, @L<strong>in</strong>eWidthEditbox_callback);<br />

Some comments are <strong>in</strong> order.<br />

• Note that <strong>the</strong> ‘Style’ is ‘edit’, so this uicontrol is a dynamic edit box, one<br />

where both user and program can modify <strong>the</strong> text <strong>in</strong>side.<br />

• The parent is <strong>the</strong> uipanel, so <strong>the</strong> position <strong>of</strong> <strong>the</strong> edit box will be measured<br />

from <strong>the</strong> lower left-hand corner <strong>of</strong> <strong>the</strong> panel with handle hPanel.<br />

• We <strong>in</strong>tialize ‘Str<strong>in</strong>g’ to ‘1’. This will be <strong>the</strong> <strong>in</strong>tial value that appears <strong>in</strong> <strong>the</strong><br />

edit box. It represents a l<strong>in</strong>e width <strong>of</strong> 1 po<strong>in</strong>t (1 pt).<br />

• We assign <strong>the</strong> function handle @L<strong>in</strong>eWidthEditbox_callback to <strong>the</strong> ‘Callback’<br />

property.<br />

At this po<strong>in</strong>t, if you attempt to modify <strong>the</strong> text <strong>in</strong> <strong>the</strong> dit box, you will<br />

receive an error as <strong>the</strong> callback function L<strong>in</strong>eWidthEditbox_callback is not<br />

yet written. Let’s take care <strong>of</strong> that immediately and write <strong>the</strong> callback.<br />

As usual, <strong>the</strong> obligatory arguments hObject and eventdata are used.

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

Saved successfully!

Ooh no, something went wrong!