GUI with figure object

This is the old way to create GUI with matlab. You can use uicontrol() function to design all kinds of buttons, text input or pushbuttons. The main window of old matlab GUI is actually a figure window. So all the button and control tools are actually operated on this figure object[1].

Basic Frames

See this minimal example[1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function SampleGUI()
clear all;close all;clc;
%global x, y, power, plot_fh, hPwr;
x = linspace(-2, 2, 100);
power = 1;
y = x.^power;
ctrl_fh = figure; % controls figure handle
plot_fh = figure; % plot figure handle
plot(x, y);
% uicontrol handles:
hPwr = uicontrol('Style','edit','Parent',...
ctrl_fh,...
'Position',[45 100 100 20],...
'String',num2str(power),...
'CallBack',@pwrHandler);

hButton = uicontrol('Style','pushbutton','Parent',ctrl_fh,...
'Position',[45 150 100 20],...
'String','Reset','Callback',@reset);

function reset(source,event,handles,varargin)% boilerplate argument string
fprintf('resetting...\n');
power=1;
set(hPwr,'String',num2str(power));
y=x.^power;
compute_and_draw_plot();
end

function pwrHandler(source,event,handles,varargin)
power=str2num(get(hPwr,'string'));
fprintf('Setting power to %s\n',get(hPwr,'string'));
compute_and_draw_plot();
end

function compute_and_draw_plot()
y=x.^power;
figure(plot_fh); plot(x,y);
end
end

From this mini example, we can clearly see that all the control buttons are build on this figure, which is an object and it is used as one of the most import inputs for all the other buttons. Then the rest work just involves how to use these control buttons.

Control buttons[2]

Label box

The main purpose of label box is to display some information for other buttons.

1
2
3
4
uicontrol('Style','text','Parent',...
fig_window,...
'Position',[0 550 60 30],...
'String','shot','FontSize', font_UI);

Text box

Receives a string and output a string.

1
2
3
4
5
% Label of shot
uicontrol('Style','text','Parent',...
fig_window,...
'Position',[0 550 60 30],...
'String','shot','FontSize', font_UI);

Push button

1
2
3
4
5
6
% Button to run inside window
uicontrol('Style','pushbutton','Parent',...
fig_window,...
'Position',[10 30 50 50],...
'String','RUN','FontSize', font_UI,...
'CallBack',@new_plot);

Push button triggers function new_plot() after any press on it.

Radio button

1
2
3
4
5
RMP_ui = uicontrol('Style','radio','Parent',...
fig_window,...
'Position',[10 200 120 30],...
'String','RMP off/on','FontSize', font_UI, ...
'HandleVisibility','off');

x = get(RMP_ui, ‘value’)
x = 1, if Radio button is check. x = 0 if Radio button is not checked. The default state is not checked.

1
2
3
4
5
6
% popup menu to choose among IRMP/phi_RMP
POP_RMP_ui = uicontrol('Style','popup','Parent',...
fig_window,...
'Position',[10 100 110 30],...
'string', {'IRMP', 'phi_RMP'}, 'FontSize', font_UI, ...
'HandleVisibility','off');

The selection value is get by:
x = get(POP_RMP_ui, ‘value’)
The value you get as x is increasing with number of selection from 1 to length(string).

Figure plot

This is rather simple. Because there is already a figure window, what you need to do rest is only to fill this figure window with subplot. But you need to set the parameter for the axes of this subplot, defining their position, grids, ticks, paras.

Reference

[1] https://www.mathworks.com/help/matlab/creating_guis/about-the-simple-programmatic-gui-example.html

[2] https://www.mathworks.com/help/matlab/ref/uicontrol.html