Read in Excel Spreadsheet as Matrix Matlab



Computing BMI – another GUI in Matlab

In this project, nosotros are going to develop a GUI in Matlab to summate the BMI (body mass index) of any people. We are going to introduce the radio button, which we haven't explored earlier in this site, and we're going to include a button to save the data in an external spreadsheet. Nosotros could use these concepts to develop circuitous databases, just we are going to keep things very simple in this case.

If yous practise not want to create a GUI but to use an online calculator, become here.


If you oasis't read the main concepts of GUI development, I suggest you read them earlier y'all continue with this experiment. Yous tin can observe here the basics of GUI development, and here the nuts of callbacks. Information technology's important that you understand those concepts first.


Notation: the BMI calculator that nosotros're going to develop hither is like an experiment with a scientific toy, and information technology's not intended for serious purposes. Use at your own take a chance.


Open the guide tool in Matlab and choose the bare GUI pick.

And then, replicate this screen and relieve your projection every bit bmi.fig.

BMI calculator - GUI in Matlab

The window is going to have iv edit boxes (or objects) to be used every bit name, age, weight and height inputs. The proper name and age are just for documentation purposes, and patently they don't affair at all for the adding process. Each edit box has an informative associated characterization. The labels can exist obtained with text objects.


This GUI is going to have a group of two radio buttons (within a panel object) to let the user select the units (anglo or metric), and information technology'due south going to have three buttons, 1 to reset or clear the input, another to perform the calculations and, the third push for saving the data in a split file, in this case it'll exist an Excel spreadsheet.

These are the components utilized in my GUI. Blank spaces mean the defaultvalue provided by Matlab:

table of elements in GUI for BMI calculation

The tag names are associated with the functions generated past the guide tool, that's why it's important to follow them. The initial strings and values are the initial atmospheric condition of the GUI and that's what the user sees when executing the script.

Initial Conditions

When you save the bmi.fig file with the objects drawn and named as detailed above, Matlab creates a kind of template. You have to "make full-in the blanks" to make it work...

Almost at the top of your new template, you lot'll see something like this office, but with less lines. Make sure that at the end, yous have all these lines...

% --- Executes just before bmi is made visible.
function bmi_OpeningFcn(hObject, eventdata, handles, varargin)
% This role has no output args, run into OutputFcn.
% hObject handle to figure
% eventdatareserved - to be divers in a time to come version of MATLAB
% handlesstructure with handles and user data (see GUIDATA)
% varargincommand line arguments to bmi (see VARARGIN)
% Cull default command line output for bmi

handles.output = hObject;

handles.metric = 0;
handles.wu =
'lb' ;
handles.hu =
'in' ;
handles.name =
'' ;
handles.age =
'' ;
handles.bmi = 0;
handles.weight =
'' ;
handles.acme =
'' ;
handles.effect =
'' ;
handles.condition =
'' ;

% Update handles construction
guidata(hObject, handles);


These variables are necessary if the user doesn't use the GUI equally intended. If the user doesn't enter information as expected and we don't apply the initial atmospheric condition, the GUI doesn't behave correctly and crashes. The weather condition above are useful to requite stability to our design.

Entering Name and Age

I'm going to mention simply the changes that you accept to make to your template. The auto-generated template contains many comments. Exit them in your lawmaking if you desire, merely I won't copy all those comments here. Use the name of the functions to follow and make the relevant modifications.

% --- Executes on text input in name_text.
function name_text_Callback(hObject, eventdata, handles)
handles.proper noun = get(hObject,
'Cord' );
guidata(hObject, handles)

% --- Executes on text input in age_text.
function age_text_Callback(hObject, eventdata, handles)
handles.age = get(hObject,
'String' );
guidata(hObject, handles)

Selecting Units

% --- Executes on button press in anglo_radio.
function anglo_radio_Callback(hObject, eventdata, handles)
handles.anglo = get(hObject,
'Value' );
set up(handles.weight_label,
'String' , 'Weight (lb):' )
set(handles.height_label,
'String' , 'Height (in):' )
prepare(handles.metric_radio,
'Value' , 0)
handles.metric = 0;
handles.wu =
'lb' ;
handles.hu =
'in' ;
guidata(hObject, handles)


% --- Executes on button press in metric_radio.

function metric_radio_Callback(hObject, eventdata, handles)
handles.metric = get(hObject,
'Value' );
set(handles.weight_label,
'Cord' , 'Weight (kg):' )
set up(handles.height_label,
'Cord' , 'Pinnacle (cm):' )
set(handles.anglo_radio,
'Value' , 0)
handles.anglo = 0;
handles.wu =
'kg' ;
handles.hu =
'cm' ;
guidata(hObject, handles)

Entering Weight and Height

% --- Executes on text input in weight_text.
function weight_text_Callback(hObject, eventdata, handles)
num = str2double(get(hObject,
'String' ));
if isnan(num)
set(hObject,
'Cord' , 0);
end
handles.weight = num;
guidata(hObject, handles)


% --- Executes on text input in height_text.
function height_text_Callback(hObject, eventdata, handles)
num = str2double(go(hObject,
'String' ));
if isnan(num)
set(hObject,
'Cord' , 0);
end
handles.height = num;
guidata(hObject, handles);

Calulating BMI

The formula for the BMI adding is

BMI = 10 four x (weight in kg) / (height in cm) ii


if you want to apply pounds and inches instead, the formula is

BMI = 10 four x (weight in lbs/2.2046) / (top in in/0.3937) ii


The table to determine the condition is

       BMI < 18.5 Underweight
      xviii.5 – 24.9 Normal
      25   – 29.9 Overweight
           > 30   Obese

The formula to summate BMI and the code to look-upwardly the table can be coded as follows:

% --- Executes on push button printing in calculate_button.
role calculate_button_Callback(hObject, eventdata, handles)
if handles.metric
westward = handles.weight;
h = handles.height;

else
w = handles.weight/ii.2046;
h = handles.peak/0.3937;

end

bmi = 1e4 * due west/h^2;

if bmi < eighteen.5
s =
' Underweight' ;
elseif eighteen.v <= bmi & bmi < 25
due south =
' Normal' ;
elseif 25 <= bmi & bmi < 30
south =
' Overweight' ;
else
southward = ' Obese' ;
end

bmis = [num2str(bmi, 3) s];
set(handles.result_text,
'String' , bmis);
handles.result = bmi;
handles.condition = due south;
guidata(hObject,handles);

Immigration the Graphic Interface

% --- Executes on button press in clear_button.
part clear_button_Callback(hObject, eventdata, handles)
set(handles.name_text,
'Cord' , '' )
handles.name =
'' ;
set(handles.age_text,
'String' , '' )
handles.historic period =
'' ;
set(handles.weight_text,
'String' , '0' )
handles.weight = 0;
set up(handles.height_text,
'String' , '0' )
handles.height = 0;
gear up(handles.result_text,
'Cord' , '' )
handles.result = 0;
handles.status =
'' ;

Saving the information in an Excel file

This process can be done in many ways. In this case we're going to go along things simple and use just the functions xlsread and xlswrite. With xlsread we're going to read the number of lines of information (registers) that the file has, and we're using xlswrite to write one more than line (register of 8 columns or fields) to the file. Here, y'all tin read a total article nearly data interchange between Matlab and Excel.

Beforehand, we're going to prepare a spreadsheet named bmi.xls, and we're going to relieve information technology in the same working directory that we're using with Matlab. A file like this:

Spreadsheet needed for BMI GUI

Nosotros take eight columns, for all the information that our graphical interface is going to produce. In cell B1, nosotros have this formula: +COUNT( G3 : G65536 ). Information technology only counts all the registers that we have in the spreadsheet. We can later hide that line (row 1) to avoid distractions.

At the get-go, the count is 0 (as shown), of grade. That means that we could add together a register in line 3, which is the kickoff line available. If we had a count of five, we could add a register in the eighth line. The count is read with Matlab congenital-in function xlsread. Then, we set up the 8-field register in a cell-array in Matlab and save it tho the canvas one 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 this lawmaking:

% --- Executes on button press in save_button.
part save_button_Callback(hObject, eventdata, handles)
d{1, 1} = handles.proper name;
d{1, 2} = handles.age;
d{i, iii} = handles.weight;
d{1, 4} = handles.wu;
d{1, v} = handles.summit;
d{1, six} = handles.hu;
d{1, 7} = handles.result;
d{one, 8} = handles.condition;

c = xlsread( 'bmi' , one , 'b1' );
position = [
'a' num2str(c+3)];
[condition, message] = xlswrite(
'bmi' , d, 1, position);

if status
helpdlg(
'Data saved ok...' , 'Save Spreadsheet' );
else
errordlg( 'Could not salvage data' , 'Save Spreadsheet' );
cease


Salvage your files and you're prepare to run it...

These are some screenshots...

One register filled-in

Data saved ok notice

Second filled-in register

Spreadsheet with data from Matlab

So the project seems to be working fine!


From 'Calculating BMI' to Matlab home

From 'Calculationg BMI' to Matlab GUIs

footer for matlab page

loftinfeling.blogspot.com

Source: https://www.matrixlab-examples.com/calculating-bmi.html

0 Response to "Read in Excel Spreadsheet as Matrix Matlab"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel