Gui With Matlab
Gui With Matlab
May 2004
Signal and Image Processing Laboratory
Written By:
Yair Moshe
1. Basic Graphics
2. Animation
3. Handle Graphics Objects
4. Creating GUI using GUIDE
1
1. Basic Graphics
• 2-D Plotting
• The Figure Window
• Data Statistics & Curve Fitting
• Subplots & Scales for Axes
• Specialized Plotting Routines
• 3-D Plotting
• Images
3
2-D Plotting
x=0:.1:2*pi;
x=0:.1:2*pi;
y=sin(x);
y=sin(x);
plot(x,y)
plot(x,y)
grid
grid onon
hold
hold onon
plot(x, exp(-x), 'r:*
plot(x, exp(-x), r:*'))
axis([0
axis([0 2*pi
2*pi 00 1])
1])
title('2-D
title( Plot'))
2-D Plot
xlabel( Time'))
xlabel( 'Time
ylabel('Sin(t)
ylabel( Sin(t) '))
text(pi/3, sin(pi/3), '<--sin(\pi/3)
text(pi/3, sin(pi/3), <--sin(\pi/3) '))
legend('Sine
legend( Wave',,
Sine Wave
'Decaying Exponential'))
Decaying Exponential
4
2
Line Characteristics
Specifier Line Color Specifier Marker Style
b blue . point
g green o circle
r red x x-mark
c cyan + plus
m magenta * star
y yellow s square
k black d diamond
v triangle down
Specifier Line Style ^ triangle up
- solid < triangle left
: dotted > triangle right
-. dashdot p pentagram
-- dashed h hexagram
3
Data Statistics & Curve Fitting
subplot(2,2,2)
subplot(2,2,2)
semilogy(x,
semilogy(x, exp(-x))
exp(-x))
subplot(2,2,3)
subplot(2,2,3)
x=0:.1:2*pi;
x=0:.1:2*pi;
plot(x,
plot(x, sin(x))
sin(x))
subplot(2,2,4)
subplot(2,2,4)
plot(peaks)
plot(peaks)
8
4
Specialized Plotting Routines
3-D Plotting
z = 0:0.1:10*pi;
x = exp(-z/20).*cos(z);
y = exp(-z/20).*sin(z);
plot3(x,y,z,'LineWidth',2)
grid on
xlabel('x')
ylabel('y')
zlabel('z')
10
5
3-D Meshes and Surfaces
11
12
6
Images
[x,map]=imread('flowers.tif');
image(x)
colormap(map)
13
Images
a = magic(4);
image(a);
map = hsv(16);
colormap(map)
colorbar
14
7
2. Animation
15
Animation
16
8
On the Fly Animation
tt == 0:0.01:10*pi;
0:0.01:10*pi; xx == t.*sin(t);
t.*sin(t); yy == t.*cos(t);
t.*cos(t);
axislimits
axislimits == [min(x)
[min(x) max(x)
max(x) min(y)
min(y) max(y)
max(y) min(t)
min(t) max(t)];
max(t)];
line_handle
line_handle == plot3(x(1),
plot3(x(1), y(1),t(1),
y(1),t(1), 'ko', ...
'MarkerFaceColor',[.49
'MarkerFaceColor',[.49 11 .63],
.63], 'MarkerSize',12);
'MarkerSize',12);
set(line_handle,
set(line_handle, 'erasemode','xor');
'erasemode','xor');
axis(axislimits);
axis(axislimits);
grid
grid on on
for
for ii == 2:length(x)
2:length(x)
set(line_handle,
set(line_handle, 'xdata',x(i), 'ydata', y(i), 'zdata', t(i));
drawnow;
drawnow;
end
end 17
[x,y]
[x,y] == meshgrid([-10:0.5:10]);
meshgrid([-10:0.5:10]);
for
for jj == 1:15
1:15
zz == bessel(0,
bessel(0, (j-1)*0.2
(j-1)*0.2 ++ sqrt(x.^2
sqrt(x.^2 +y.^2));
+y.^2));
surf(x,y,z)
surf(x,y,z)
axis([-10
axis([-10 1010 -10
-10 10
10 -.5
-.5 1])
1])
M(j)
M(j) == getframe;
getframe;
end
end
frame_order
frame_order == [1:15
[1:15 14:-1:1];
14:-1:1];
number_repeats
number_repeats == 5;5;
movie(M,
movie(M, [number_repeats
[number_repeats frame_order]);
frame_order]);
18
9
3. Handle Graphics
19
20
10
Graphics Objects Hierarchy
21
Surface
Object UIControl
Objects
Patch
Object
Text
Objects
22
11
Obtaining an Object’s Handle
23
24
12
Example – Figure Position
space
space == 5;
5;
top_space
top_space == 80;
80;
scn_size
scn_size == get(0,'ScreenSize');
get(0,'ScreenSize');
pos1
pos1 == [space,
[space, 2/3*scn_size(4)
2/3*scn_size(4) ++ space,...
space,...
scn_size(3)/2
scn_size(3)/2 -- 2*space,
2*space, scn_size(4)/3
scn_size(4)/3 -- (top_space
(top_space ++ space)];
space)];
pos2
pos2 == [pos1(1)
[pos1(1) ++ scn_size(3)/2,
scn_size(3)/2, pos1(2),...
pos1(2),...
pos1(3),
pos1(3), pos1(4)];
pos1(4)];
h1
h1 == figure(1);
figure(1);
peaks;
peaks;
h2
h2 == figure(2);
figure(2);
membrane;
membrane;
set(h1,
set(h1, 'Position',
'Position', pos1)
pos1)
set(h2,
set(h2, 'Position',
'Position', pos2)
pos2)
25
• What is GUIDE?
• Creating a GUI
• The Layout Editor
• Hands-On GUIDE Example
• Writing Callbacks
26
13
What is GUIDE?
27
Creating a GUI
28
14
The Layout Editor
Alignment Menu
The Layout Editor looks like this: Tool Editor
M-File
Editor
Run Button
Component
Palette Object Browser
Property
Inspector
Layout Area
Figure
Resize Tab
29
Example:
30
15
Writing Callbacks
A callback is a sequence of commands that are execute when a graphics
object is activated
• Stored in the GUI’s M-file
• Is a property of a graphics object (e.g. CreateFnc, ButtonDwnFnc,
Callback, DeleteFnc)
• Also called event handler in some programming languages
31
Writing Callbacks
%% Get
Get user
user input
input from
from GUI
GUI
f1
f1 == str2double(get(handles.f1_input,'String'));
str2double(get(handles.f1_input,'String'));
f2
f2 == str2double(get(handles.f2_input,'String'));
str2double(get(handles.f2_input,'String'));
tt == eval(get(handles.t_input,'String'));
eval(get(handles.t_input,'String'));
%
% Calculate
Calculate data
data
…
…
%
% Create
Create frequency
frequency plot
plot
axes(handles.frequency_axes)
axes(handles.frequency_axes) %% Select
Select the
the proper
proper axes
axes
plot(f,m(1:257))
plot(f,m(1:257))
xlabel('Frequency');
xlabel('Frequency');
set(handles.frequency_axes,'XMinorTick','on')
set(handles.frequency_axes,'XMinorTick','on')
grid
grid on
on 32
…
…
16
Further Information
33
34
17