53
More Matlab Graphics More Matlab Graphics and GUI and GUI Graphics subplots some useful commands 3D graphics GUI GUI controls The callback property Other essential properties

More Matlab Graphics and GUI

Embed Size (px)

DESCRIPTION

More Matlab Graphics and GUI. Graphics subplots some useful commands 3D graphics GUI GUI controls The callback property Other essential properties. Comments about the “Game of Life”. Initial universe. Time step. Time step. input universe. embed. Count neighbors. - PowerPoint PPT Presentation

Citation preview

Page 1: More Matlab Graphics and GUI

More Matlab Graphics and More Matlab Graphics and GUIGUI

Graphics subplots some useful commands 3D graphics

GUI GUI controls The callback property Other essential properties

Page 2: More Matlab Graphics and GUI

Comments about the “Game of Life”

Initial universe

Time step

Page 3: More Matlab Graphics and GUI

Time step

input universe

embed

output universe

Count neighbors

Make decisions

Page 4: More Matlab Graphics and GUI

Count neighbors

input universe

Count the neighbors of a cell

Matrix with the neighbors numbers

Page 5: More Matlab Graphics and GUI

Subplots

Subplot I

Subplot II

Page 6: More Matlab Graphics and GUI

h1 = subplot(7,7,[1 2 3 4 5 6 ... 8 9 10 11 12 13 ... 15 16 17 18 19 20 ... 22 23 24 25 26 27 ... 29 30 31 32 33 34 ... 36 37 38 19 40 41]);

1234567

891011121314

15161718192021

22232425262728

29303132333435

36373839404142

43444546474849

Page 7: More Matlab Graphics and GUI

h1 = subplot(7,7,[1 2 3 4 5 6 ... 8 9 10 11 12 13 ... 15 16 17 18 19 20 ... 22 23 24 25 26 27 ... 29 30 31 32 33 34 ... 36 37 38 19 40 41]);

get(h1,'type')ans =axes

Page 8: More Matlab Graphics and GUI

h2=axes

Page 9: More Matlab Graphics and GUI

subplot(7,7,[7 14])

Page 10: More Matlab Graphics and GUI

subplot(7,7,[7 14])

Page 11: More Matlab Graphics and GUI

subplot(7,7,[7 14])

h1 = subplot(7,7,[1 2 3 4 5 6 ... 8 9 10 11 12 13 ... 15 16 17 18 19 20 ... 22 23 24 25 26 27 ... 29 30 31 32 33 34 ... 36 37 38 19 40 41]);

1234567

891011121314

15161718192021

22232425262728

29303132333435

36373839404142

43444546474849

Page 12: More Matlab Graphics and GUI

The interactively edited figure/axes/object is

accessible through the gcf/gca/gco commands (get current figure/axes/object).

>> get(gca)ALim = [0 1]ALimMode = autoAmbientLightColor = [1 1 1]Box = onCameraPosition = [5.5 5.5 17.3205]CameraPositionMode = autoCameraTarget = [5.5 5.5 0]CameraTargetMode = autoCameraUpVector = [0 1 0]CameraUpVectorMode = autoCameraViewAngle = [6.60861]CameraViewAngleMode = auto

:

Useful Commands

Page 13: More Matlab Graphics and GUI

plot(x,y)

Page 14: More Matlab Graphics and GUI

hl=findobj(gcf,’type’,’line’)

Page 15: More Matlab Graphics and GUI

hl=findobj(gcf,’type’,’line’)

set(h1,'color','r');

Page 16: More Matlab Graphics and GUI

set(gcf,'tag','first‘);

Figure;

y = x.^-2;

plot(x,y);

Page 17: More Matlab Graphics and GUI

firstFigure = findobj('tag','first');

h2 = findobj(firstFigure,'type','line')

set(h2,'marker','*');

Page 18: More Matlab Graphics and GUI

firstFigure = findobj('tag','first');

h2 = findobj(firstFigure,'type','line')

set(h2,'marker','*');

Page 19: More Matlab Graphics and GUI

3D graphics

x = [-2:0.2:2]

x = [ -2 -1.8 …. 1.8 2]

y = [-4:0.4:4]

y = [ -4 -3.6 …. 3.6 4]

We want to calculate and display Z=x(x2-y2)

Page 20: More Matlab Graphics and GUI

3D graphics

surf(XX,YY,ZZ)

size(XX)ans = 21 21

XX(1:5,1:5)ans = -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …... replica of x

length(y)rows

Page 21: More Matlab Graphics and GUI

surf(XX,YY,ZZ)

size(XX)ans = 21 21

XX(1:5,1:5)ans = -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …...

Page 22: More Matlab Graphics and GUI

surf(XX,YY,ZZ)

size(XX)ans = 21 21

XX(1:5,1:5)ans = -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …. -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 …...

Page 23: More Matlab Graphics and GUI

3D graphics

surf(XX,YY,ZZ)

size(YY)ans = 21 21

YY(1:5,1:5)ans = -4.0000 -4.0000 -4.0000 -4.0000 -4.0000 …. -3.6000 -3.6000 -3.6000 -3.6000 -3.6000 …. -3.2000 -3.2000 -3.2000 -3.2000 -3.2000 …. -2.8000 -2.8000 -2.8000 -2.8000 -2.8000 …. -2.4000 -2.4000 -2.4000 -2.4000 -2.4000 …. . .

replica of y

Length(x) columns

Page 24: More Matlab Graphics and GUI

3D graphics

surf(XX,YY,ZZ)

size(ZZ)ans = 21 21

ZZ = XX.*(XX.^2-YY.^2);

Element-by-element matrix operations

Page 25: More Matlab Graphics and GUI

How do we create XX and YY?

[XX YY] = meshgrid(x,y);

Were [XX YY] = meshgrid(x) is the same as [XX YY] = meshgrid(x,x);

Page 26: More Matlab Graphics and GUI

How does Matlab know how to relate some How does Matlab know how to relate some value with a specific colorvalue with a specific color??

Colormaps! A colormap is an m-rows by 3 column matrix Each row is a color specified in RGB values (between 0 and 1)

C=get(1,'colormap')

C =

0 0 0.5625 0 0 0.6250 ... ... ... 0.5625 1.0000 0.4375 0.6250 1.0000 0.3750

... ... ... 0.6250 0 0 0.5625 0 0 0.5000 0 0

Page 27: More Matlab Graphics and GUI

colormap(cool)

“colormap” is a property of the figure

Page 28: More Matlab Graphics and GUI

colormap(colorcube)

Page 29: More Matlab Graphics and GUI

The same plot as a contour map

colormap(jet)contour(XX,YY,ZZ,100)

Page 30: More Matlab Graphics and GUI

… and with a colorbar

colorbar

Page 31: More Matlab Graphics and GUI

Solid shadingSolid shading

We can change the way the surface displays by changing its shading:

>>shadingflatfaceted %defaultinterp

>>surf(peaks) %creates 49 by 49 matrix % an plots its values %against x,y indices

flat

faceted

interpolated

Page 32: More Matlab Graphics and GUI

Every axes has a property called ‘view’ that controls the azimuth (az) and elevation (el) of the viewpoint

To change the viewpoint we use either:

view(az,el) or

set(axes_handle,’View’,[az el])

Page 33: More Matlab Graphics and GUI

>>view(-37.5,30) >>view(-10.0,30)

>>view(0,30)>>view(0,0)

Page 34: More Matlab Graphics and GUI

Special 3d plotsSpecial 3d plots

contour3(x,y,z,n) %create n realistic contours interpreting z

%as the height from the x y plane. If specified

% x and y should have the same size as z

>>contour3(topo,30)

>>axis off

Page 35: More Matlab Graphics and GUI

Special 3d plotsSpecial 3d plotsWe can create a sphere or a cylinder (of radius =1) with little

effort

>>[x,y,z]=sphere; %will compute the required % N+1 (N=20 by default) coordinates so

>>surf(x,y,z) %will plot sphere surface

>>axis equal

Note:

sphere without left arguments will plot the surface.

sphere(n) use n instead of the default of 20

Page 36: More Matlab Graphics and GUI

Special 3d plotsSpecial 3d plotsTo create a cylinder we issue the following command:

>>[x,y,z]=cylinder;

>>surf(x,y,z)%this creates a basic cylinder of %radius 1

Page 37: More Matlab Graphics and GUI

We can also create cylinder with reference to a specific profile. We will now generate a cylinder with the esin(t) profile

>> t = 0:pi/10:2*pi;

>> [X,Y,Z] = cylinder(exp(sin(t)));

>> surf(X,Y,Z)

Special 3d plotsSpecial 3d plots

Page 38: More Matlab Graphics and GUI

Graphical User Interfaces

Why?

Built in UIs

Uicontrols

GUI programming

Page 39: More Matlab Graphics and GUI

Every day examples

Page 40: More Matlab Graphics and GUI

GUI examples

Page 41: More Matlab Graphics and GUI
Page 42: More Matlab Graphics and GUI
Page 43: More Matlab Graphics and GUI
Page 44: More Matlab Graphics and GUI

Graphic Objects

• figure• axes• 2D-plot• 3D-plot• axis labels• title

GUI objects• pushbutton• toggle• edit• text• menu

Page 45: More Matlab Graphics and GUI

1. Enable you to share your work with other people (your advisor/lab mates or grandmother)

2. Easy way to perform repetitive task that need constant user input

3. Create interactive demos to your applications

4. It’s fun…

Motivation or why GUIs?

Page 46: More Matlab Graphics and GUI

GUI components

Figure window

GUI controllers which are divided into two major classes:

uimenu (like the File,Edit etc)

uicontrols (buttons, lists, radio buttons pop-up list like in any other application)

uimenu and uicontrols are children of the figure

If an uimenu is implemented as submenu, then it is a child of another uimenu

Page 47: More Matlab Graphics and GUI

The UIcontrols are common interface controlers

Help us perform specific actions or set the variables for future actions

Actions and options are selected by the mouse, some UIcontrols are also editable so we can use the keyboard as well

Interface controllers

Page 48: More Matlab Graphics and GUI

Interface controllers

We can create UIcontrols simply by implementing the following syntax

handle_to_UI=uicontrol(‘Property Name’,’Property Value’);

Page 49: More Matlab Graphics and GUI

Interface controllers

UIcontrol types are defined by their ‘style’ property:

Check box 'checkbox'Editable text field 'edit'

Toggle button 'toggle' 'Pop-up menus 'popupmenu'

Push button 'pushbuttonList box 'listbox'

Radio button 'radiobutton'

Static text 'text'

Slider 'slider'Frame 'frame'

Page 50: More Matlab Graphics and GUI

Interface controllersare graphic objects

Essential properties:

Callback – A string with one or more commands that is executed when the controller is activated.

May be empty – ''

Page 51: More Matlab Graphics and GUI

Example uicontrol('style','pushbutton','callback','close(gcf)');

Page 52: More Matlab Graphics and GUI

Interface controllersare graphic objects

Essential properties:

Callback – A string with one or more commands that is executed when the controller is activated.

May be empty - ''

Recommendation – always call a function that does what you want.

Page 53: More Matlab Graphics and GUI

Interface controllersare graphic objects

Essential properties:

Callback – A string with one or more commands that is executed when the controller is activated.

May be empty - ''

Recommendation – always call a function that does what you want.

Tag – a string that may be used as a unique identifier of the controller.

h = findobj('Tag','1');