BMI – another GUI in Matlab
project, we are going to develop a GUI in Matlab to calculate the BMI
mass index) of any people. We are going to introduce the radio button,
haven’t explored before in this site, and we’re going to include a
save the data in an external spreadsheet. We could use these concepts
develop complex databases, but we are going to keep things very simple
If you do not want to create a GUI but to use an online calculator, go here.
haven’t read the main concepts of GUI development, I suggest you read
before you continue with this experiment. You can find here
the basics of GUI
development, and here the basics of callbacks.
It’s important that you
understand those concepts first.
the BMI calculator that we’re going to develop here is like an
a scientific toy, and it’s not intended for serious purposes. Use at your
the guide tool in Matlab and choose the blank GUI option.
this screen and save your project as bmi.fig.
window is going to have four edit boxes (or objects) to be used as
weight and height inputs. The name and age are just for documentation
and obviously they don’t matter at all for the calculation process.
box has an informative associated label. The labels can be obtained
GUI is going to have a group of two radio buttons (within a panel
let the user select the units (anglo or metric), and it’s going to have
buttons, one to reset or clear the input, another to perform the
and, the third button for saving the information in a separate file, in
case it’ll be an Excel spreadsheet.
are the components utilized in my GUI. Blank spaces mean the
provided by Matlab:
names are associated with the functions generated by the guide tool,
it’s important to follow them. The initial strings and values are the
conditions of the GUI and that’s what the user sees when executing the
you save the bmi.fig file with the objects drawn and named as detailed
Matlab creates a kind of template. You have to “fill-in the blanks” to
at the top of your new template, you’ll see something like this
with less lines. Make sure that at the end, you have all these
--- Executes just before bmi is made visible.
bmi_OpeningFcn(hObject, eventdata, handles, varargin)
This function has no output args, see OutputFcn.
eventdata reserved -
to be defined in a
future version of MATLAB
with handles and
user data (see GUIDATA)
varargin command line
arguments to bmi
Choose default command line output for bmi
Update handles structure
variables are necessary if the user doesn’t use the GUI as intended. If
user doesn’t enter data as expected and we don’t use the initial
the GUI doesn’t behave correctly and crashes. The conditions above are
to give stability to our design.
Name and Age
to mention only the changes that you have to make to your template. The
auto-generated template contains many comments. Leave them in your code
want, but I won’t copy all those comments here. Use the name of the
to follow and make the relevant modifications.
--- Executes on text input in name_text.
name_text_Callback(hObject, eventdata, handles)
--- Executes on text input in age_text.
age_text_Callback(hObject, eventdata, handles)
--- Executes on button press in anglo_radio.
anglo_radio_Callback(hObject, eventdata, handles)
'String', 'Weight (lb):')
'String', 'Height (in):')
--- Executes on button press in metric_radio.
metric_radio_Callback(hObject, eventdata, handles)
Weight and Height
--- Executes on text input in weight_text.
weight_text_Callback(hObject, eventdata, handles)
--- Executes on text input in height_text.
height_text_Callback(hObject, eventdata, handles)
The formula for the BMI
BMI = 104 x (weight in kg) / (height
if you want to use pounds and inches instead, the formula is
BMI = 104 x (weight in lbs/2.2046) /
The table to determine the condition is
BMI < 18.5 Underweight
18.5 – 24.9
25 – 29.9 Overweight
The formula to calculate
BMI and the code to look-up the
table can be coded as follows:
--- Executes on button press in calculate_button.
calculate_button_Callback(hObject, eventdata, handles)
w = handles.weight/2.2046;
1e4 * w/h^2;
<= bmi & bmi < 25
<= bmi & bmi < 30
s = ' Obese';
[num2str(bmi, 3) s];
Clearing the Graphic Interface
--- Executes on button press in clear_button.
clear_button_Callback(hObject, eventdata, handles)
Saving the data in an Excel file
process can be done in many ways. In this case we’re
going to keep things simple and use just the functions xlsread and
With xlsread we’re going to read the number of lines of information
that the file has, and we’re using xlswrite to write one more line
eight columns or fields) to the file. Here, you can read a full article about
data interchange between Matlab and Excel.
Beforehand, we’re going
to prepare a spreadsheet named
bmi.xls, and we’re going to save it in the same working directory that
using with Matlab. A file like this:
We have eight columns,
for all the information that our
graphical interface is going to produce. In cell B1, we have this
formula: +COUNT( G3 : G65536 ). It just counts all the registers that
we have in
the spreadsheet. We can later hide that line (row 1) to avoid
At the beginning, the
count is 0 (as shown), of course. That
means that we could add a register in line 3, which is the first line
available. If we had a count of 5, we could add a register in the 8th line. The count is
read with Matlab built-in function
xlsread. Then, we prepare the 8-field register in a cell-array in
save it tho the sheet 1 of the file ‘bmi.xls’.
We can even think of a
pop-up window to display success or
failure of the saving process...
That’s accomplished with
--- Executes on button press in save_button.
save_button_Callback(hObject, eventdata, handles)
xlsread('bmi', 1 , 'b1');
= ['a' num2str(c+3)];
message] = xlswrite('bmi', d, 1, position);
saved ok...', 'Save
not save data', 'Save
Save your files and you’re ready to run it...
These are some
So the project seems to
be working fine!
'Calculating BMI' to Matlab home
'Calculationg BMI' to Matlab GUIs