Collated Advice On Construction of User Interfaces: Matlab Gui Tips
Collated Advice On Construction of User Interfaces: Matlab Gui Tips
R. S. Schestowitz
Imaging Science and Biomedical Engineering
Stopford Building
University of Manchester
United Kingdom
1
Contents
1 Introduction 3
2 Starting Point 3
3.4 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 GUI Editing 7
5 Final Word 7
2
1 Introduction
2 Starting Point
Let us assume that a function myf unction has been created and it takes
a series of arguments (e.g., arg1,arg2 and so on). A call to this function
will either produce a result (visual or numerical typically) or return
data of some sort to the command-line (or command window). What is
the difference between the two? Simply, the former makes the function
a stand-alone list of commands that cannot be easily combined with
other code. In the case of GUI’s, these usually appear the appropri-
ate entity to start off with. The exception is the case where our GUI
should be used by another auxiliary application; but this case will not
be considered here any further.
3
Let us illustrate with an example. The function draw_some_plot is is
set to produce a plot and do nothing more. The code for such a file
(draw_some_plot.m) may be:
plot(rand(10));
draw_some_plot;
which will then draw the plot, assuming paths have been defined prop-
erly. We now have covered some dry and embarrassingly fundamental
grounds. The next few section describe some of the interesting things
that can be done to increase the functionality of the user interface
quickly and wisely (in practice, I never got that far).
The following aims to present some code which is repeatedly used and
establishes what is expected from objects.
4
3.1 Radio Buttons
where option1 is the source of the callback. For the other options, the
only part which needs changing is the latter one where the state of the
button is defined by a zero or a one.
if (get(handles.state,’Value’) == 0),
set(handles.checkbox, ’Value’, 0);
set(handles.state, ’String’, ’0’);
else
set(handles.checkbox, ’Value’, 1);
set(handles.state, ’String’, ’1’);
end
contents = get(hObject,’String’);
5
Subsequently, the menu entry needs to be checked for extraction and
setting of information.
if (strcmp(contents{get(hObject,’Value’)},’MenuEntry1’)),
set(handles.data, ’String’, ’Data1’);
elseif (strcmp(contents{get(hObject,’Value’)},’MenuEntry2’)),
set(handles.data, ’String’, ’Data2’);
else
msgbox(’Error with menu callback. Parameter passed is not recog
end
3.4 Sliders
The following code will fetch the quantised value of the slider and as-
sign it to a new object called slider_value.
For items that need to be ticked and unticked, the following can be
useful.
6
4 GUI Editing
More efficient work on the code can be done by opening all relevant files
in advance. I personally write M-files to open all relevant windows at
the start. Set up a file which includes the following lines:
where of course the files listed are these which are frequently worked
upon.
5 Final Word
The document was adapted from notes whose essence is given in Sec-
tion 3 onwards. As I earlier stated, it is meant to be useful more than
eloquent and hopefully assist people who are new to callback function
constructions. Comments are very welcome, most preferably via E-
mail.