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.

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

This function takes no <strong>in</strong>put and export no output. However, it is possible to<br />

write GUI functions that accept <strong>in</strong>put and export data back to <strong>the</strong> caller.<br />

Next we close all open figure w<strong>in</strong>dows and clear <strong>the</strong> command w<strong>in</strong>dow. This<br />

is helpful dur<strong>in</strong>g <strong>the</strong> development <strong>of</strong> <strong>the</strong> GUI, but once we have a tested and<br />

work<strong>in</strong>g program, we’ll remove <strong>the</strong>se two commands from <strong>the</strong> GUI.<br />

We set limits on <strong>the</strong> doma<strong>in</strong> <strong>of</strong> our plot, which we will also use when we<br />

<strong>in</strong>itialize an axes on our figure w<strong>in</strong>dow. It’s helpful to put <strong>the</strong>se here and not<br />

have to change <strong>the</strong>m <strong>in</strong> several places <strong>in</strong> our program.<br />

F<strong>in</strong>ally, we <strong>in</strong>itialize some colors we will use for our GUI components. Note<br />

that each color takes <strong>the</strong> form <strong>of</strong> a row vector with three entries, each <strong>of</strong> which<br />

must be a number between 0 and 1. The first entry is <strong>the</strong> percentage <strong>of</strong> red,<br />

<strong>the</strong> second <strong>the</strong> percentage <strong>of</strong> green, and <strong>the</strong> third entry is <strong>the</strong> percentage <strong>of</strong> blue.<br />

Most modern computers have color wheels where you can view a color and <strong>the</strong><br />

percentages <strong>of</strong> red, green, and blue (<strong>in</strong> <strong>the</strong> RGB system) to make up <strong>the</strong> color.<br />

Next, we’ll add a figure w<strong>in</strong>dow to <strong>the</strong> GUI. Add <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es to <strong>the</strong><br />

function M-file plotGUI and save.<br />

hFigure=figure(...<br />

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

’Position’, [100 100 700 500],...<br />

’Color’,figure_color,...<br />

’MenuBar’,’none’,...<br />

’ToolBar’,’none’,...<br />

’NumberTitle’,’<strong>of</strong>f’,...<br />

’Name’,’Plot GUI’);<br />

Run <strong>the</strong> program. Some th<strong>in</strong>gs to note:<br />

1. We set Units to Pixels. Thereafter, all position<strong>in</strong>g commands are made <strong>in</strong><br />

Pixels. Th<strong>in</strong>k <strong>of</strong> a pixel as one dot on your computer screen. Typical resolution<br />

on modern displays are <strong>of</strong> <strong>the</strong> order <strong>of</strong> 1024 by 768 pixels, but can be higher<br />

(or lower) depend<strong>in</strong>g on personal display sett<strong>in</strong>gs on your computer.<br />

2. Next we set <strong>the</strong> Position <strong>of</strong> <strong>the</strong> figure w<strong>in</strong>dow at [100 100 700 500]. The<br />

first two measurements (100 and 100) are <strong>the</strong> location <strong>of</strong> <strong>the</strong> lower left-hand<br />

corner <strong>of</strong> <strong>the</strong> figure w<strong>in</strong>dow, relative to <strong>the</strong> lower left-hand corner <strong>of</strong> your<br />

computer screen. The next two measurements (700 and 500) are <strong>the</strong> width<br />

and height <strong>of</strong> <strong>the</strong> figure w<strong>in</strong>dow. All <strong>of</strong> <strong>the</strong>se measurements are <strong>in</strong> pixels<br />

3. F<strong>in</strong>ally, we set <strong>the</strong> color, <strong>the</strong>n turn <strong>of</strong>f <strong>the</strong> menubar and toolbar as well as<br />

<strong>the</strong> usual number title that is displayed <strong>in</strong> a typical figure w<strong>in</strong>dow (Figure 1,

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

Saved successfully!

Ooh no, something went wrong!