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> 403<br />

>> varScope2<br />

In nestfun1, x equals: 3<br />

Very <strong>in</strong>terest<strong>in</strong>g! No global variables, no pass<strong>in</strong>g <strong>of</strong> values as arguments, but<br />

because nestfun1 is “nested” with<strong>in</strong> <strong>the</strong> primary function, <strong>the</strong> nested function<br />

nestfun1 has access to all variables assigned <strong>in</strong> <strong>the</strong> primary function.<br />

Graphical User Interfaces<br />

In this section we <strong>in</strong>troduce our readers to <strong>Matlab</strong>’s GUI’s Graphical User Interfaces).<br />

<strong>Matlab</strong> has a full complement <strong>of</strong> uicontrols (User Interface Controls),<br />

typical <strong>of</strong> what you commonly see <strong>in</strong> most <strong>of</strong> today’s modern s<strong>of</strong>ware: edit boxes,<br />

popupmenus, push- and toggle-buttons, sliders, radio buttons, and checkboxes, a<br />

few <strong>of</strong> which we will <strong>in</strong>troduce <strong>in</strong> this section.<br />

We’re go<strong>in</strong>g to write our first GUI, keep<strong>in</strong>g it low-key as we <strong>in</strong>troduce <strong>the</strong><br />

concepts. Our gui design can be described with <strong>the</strong> follow<strong>in</strong>g series <strong>of</strong> tasks:<br />

1. Plot a function.<br />

2. Provide radio buttons to change <strong>the</strong> color <strong>of</strong> <strong>the</strong> graph <strong>of</strong> <strong>the</strong> function, <strong>of</strong>fer<strong>in</strong>g<br />

choices <strong>of</strong> red, green, or blue.<br />

3. Provide a popup menu with choices <strong>of</strong> different l<strong>in</strong>e styles for <strong>the</strong> plot: solid,<br />

dotted, etc.<br />

4. Provide an edit box that allows <strong>the</strong> user to change <strong>the</strong> l<strong>in</strong>ewidth.<br />

Let’s beg<strong>in</strong>. Open <strong>the</strong> editor and enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es, <strong>the</strong>n save <strong>the</strong><br />

function M-file as plotGUI.m.<br />

function plotGUI<br />

close all<br />

clc<br />

% plot doma<strong>in</strong><br />

xm<strong>in</strong>=0;<br />

xmax=4*pi;<br />

% some colors<br />

figure_color=[0.8,0.9,0.8];<br />

panel_color=[1,0.9,0.8];<br />

buttongroup_color=[0.9,0.9,0.8];

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

Saved successfully!

Ooh no, something went wrong!