Building Gui & Solving Odes: Introduction To Matlab & Data Analysis
Building Gui & Solving Odes: Introduction To Matlab & Data Analysis
Lecture 11:
Building GUI &
Solving ODEs
Todays menu
Previous
lecture
3
%getstringlengths
n1=length(str1);
n2=length(str2);
n=min(n1,n2);
%findcharactersthatdiffer
k=find(str1(1:n)~=str2(1:n));
ifisempty(k)%ifallcharactersareidenticalthencomparelengths
res=sign(n1n2);
else
%comparefirstcharacterthatisdifferent
k=k(1);
res=sign(str1(k)str2(k));
end
Building a GUI
Building a GUI
Lets build the following simple
Building a GUI
Building a GUI
You should see the following screen appear.
Choose the first option Blank GUI (Default).
Building a GUI
You should now see the following screen
(or something similar depending on what version of MATLAB you are
using and what the predesignated settings are):
Building a GUI
Before adding components blindly, it is good to have a rough idea about
how you want the graphical part of the GUI to look like so that itll be
easier to lay it out. Below is a sample of what the finished GUI might look
like.
Edit
Edit text
text
Static
Static text
text
Push
Push button
button
Edit text
Building a GUI
Creating the Visual Aspect of the GUI: Part
1
1. For the adder GUI, we will need the following
components
Two Edit Text components
Three Static Text component
One Pushbutton component
Building a GUI
Add in all these components to the GUI by clicking on the icon and placing it
onto the grid. At this point, your GUI should look similar to the figure below
Building a GUI
Next, its time to edit the properties of these components. Lets
start with the static text.
Double click one of the Static Text components.
You should see the following table appear.
It is called the Property Inspector and it allows you to modify the
properties of a component.
Building a GUI
Were interested in changing the String parameter. Go ahead and edit this te
Building a GUI
Lets also change the font size from 8 to 20.
Building a GUI
Now, do the same for the next Static Text component, but
instead of changing the String parameter to +, change it to =.
For the third Static Text component, change the String
parameter to whatever you want as the title to your GUI.
I kept it simple and named it MyAdderGUI. You can also
experiment around with the different font options as well.
Building a GUI
For the final Static Text component, we want to set the String Parame
In addition, we want to modify the Tag parameter for this componen
The Tag parameter is basically the variable name of this component.
Lets call it answer_staticText. This component will be used to display
our answer, as you have probably already have guessed.
Building a GUI
So now, you should have something that looks like
the following:
Building a GUI
Next, lets modify the Edit Text components. Double click on the
first Edit Text component. We want to set the String parameter to 0
and we also want to change the Tag parameter to input1_editText,
as shown below. This component will store the first of two numbers
that will be added together.
Building a GUI
For the second Edit Text component, set the String parameter
to 0 BUT set the Tag parameter input2_editText. This
component will store the second of two numbers that will be
added together.
Building a GUI
Finally, we need to modify the pushbutton component. Change the
String parameter to Add! and change the Tag parameter to
add_pushbutton. Pushing this button will display the sum of the two
input numbers.
Building a GUI
So now, you should have something like this:
Building a GUI
Rearrange your components accordingly. You should have
something like this when you are done:
Building a GUI
Now, save your GUI under any file name you please. I chose to name
mine myAdder.
When you save this file, MATLAB automatically generates two files:
myAdder.fig and myAdder.m.
The .fig file contains the graphics of your interface. The .m file
contains all the code for the GUI.
Building a GUI
Writing the Code for the GUI Callbacks
MATLAB automatically generates an .m file to go along with the
figure that you just put together. The .m file is where we attach
the appropriate code to the callback of each component. For
the purposes of this tutorial, we are primarily concerned only
with the callback functions. You dont have to worry about any
of the other function types.
Building a GUI
Open up the .m file that was automatically generated when you saved
your GUI.
In the MATLAB editor, click on the
functions
within the .m file. Select input1_editText_Callback.
Building a GUI
The cursor should take you to the following code block:
Building a GUI
Add the following code to the bottom of that code block:
Building a GUI
This piece of code simply makes sure that the input is well defined. We
dont want the user to put in inputs that arent numbers! The last line
of code tells the gui to update the handles structure after the callback
is complete. The handles stores all the relevant data related to the GUI.
This topic will be discussed in depth in a different tutorial. For now, you
should take it at face value that its a good idea to end each callback
function with guidata(hObject, handles); so that the handles are always
updated after each callback. This can save you from potential
headaches later on.
Building a GUI
Add the same block of code to input2_editText_Callback.
Now we need to edit the add_pushbutton_Callback. Click on the icon
and select add_pushbutton_Callback. The following code block is what
you should see in the .m file
Building a GUI
Building a GUI
Lets discuss how the code we just added works:
The two lines of code above take the strings within the Edit Text
components, and stores them into the variables a and b. Since they are
variables of String type, and not Number type, we cannot simply add
them together. Thus, we must convert a and b to Number type before
MATLAB can add them together.
Building a GUI
We can convert variables of String type to Number type using the
MATLAB command str2num(String argument). Similarly, we can do
the opposite using num2str(Number argument). The following line
of code is used to add the two inputs together.
The reason we convert the final answer back into String type is
because the Static Text component does not display variables of
Number type. If you did not convert it back into a String type, the
GUI would run into an error when it tries to display the answer.
Building a GUI
Now we just need to send the sum of the two inputs to the answer box that
we created. This is done using the following line of code. This line of code
populates the Static Text component with the variable c.
Building a GUI
Congratulations, were finished coding the GUI. Dont forget to
save your m-file. It is now time to launch the GUI!
If you dont want MATLAB to automatically generate all those
comments for each of the callbacks, there is a way to disable this
feature. From the GUI editor, go to File, then to Preferences
Building a GUI
icon on the