You are on page 1of 24

Imam Ja'afarAl-SadiqUniversity / Kirkuk

Computer Technical Engineering


Second Class

Computer Application - 9
Dr. Yousif A. Hamad
‫برامج أضافية‬
• "if.....end'' Structures
if ...... end
The simplest form of if statement is
if logical_ expression
statement 1
statement 2
……
end
Example:
a = 5;
if (a > 0 )
b = a;
disp (' a is positive')
end

2
if ... else ... end
The simplest form of if …. else statement is
if logical_ expression
block of statements
evaluated if TRUE
else
block of statements
evaluated if FALSE
end

Example:
Write a program to print ( above boiling and toohigh=1 ) when
temperature greater than 100 otherwise print (temperature is OK and
toohigh = 0), where the input temperature is equal to 150 (try also
using 80).

3
>> temperature = 150;
>> if ( temperature > 100)
disp (' Above boiling.')
toohigh = 1
else
disp ('Temperature is OK.')
toohigh=0
end
• if ... elseif ... else ... end
The simplest form of if …. else statement is
if logical_ expression1
block of statements evaluated
if logical_ expression1 is TRUE
elsif logical_ expression2
block of statements evaluated
if logical_ expression2 is TRUE
else
block of statements evaluated
if no other exoressions is TRUE
4
end
Example:
Write a program to print (very tall ) when the height is greater than 190
or print (tall) if height greater than (170) or print (small) when the
height less than (150), else print average, where the input height = 200
(try also using 180 or 130).

>> height = 200;


if (height > 190)
disp ('very tall')
elseif (height > 170)
disp ( ' tall ')
elsif ( height < 150 )
disp ( ' small ')
else
disp ( ' average ')
end
5
•"for...end'' Loop
In the for ... end loop, the execution of a command is repeated at a
fixed and predetermined number of times. The syntax is
for variable = expression
statements
end

•Nested loop
MATLAB also allows to use one loop inside another loop. The syntax
for a nested for loop statement in MATLAB is as follows:
for m = 1:j
for n = 1:k
<statements>;
end
end

6
•Switch ....Case
The switch statement executes certain statements based on the value of
variable or expression.
switch switch_expr
case case_expr1
commands ...
case {case_expr2,case_expr3}
commands ...
otherwise
commands ...
end

7
Relational and Logical Operators
A relational operator compares two numbers by determining whether a
comparison is true or false. Relational operators are shown in the
Table.

8
• A graphical user interface (GUI) is a graphical
interface to a program, that enable a user to perform
interactive tasks. To perform these tasks, the user of the
GUI does not have to create a script or type commands at
the command line. Often, the user does not have to know
the details of the task at hand. A good GUI can make
programs easier to use by providing them with a consistent
appearance and with intuitive controls like pushbuttons, list
boxes, sliders, menus, and so forth. The GUI should behave
in an understandable and predictable manner, so that a user
knows what to expect when performs an action.

1
• Initializing GUIDE (GUI Creator)
•First, open up MATLAB. Go to the command window and type in
guide.

• You should see the following screen appear. Choose the first option
Blank GUI (Default).

2
•You should now see the following screen (or something similar
depending on what version of MATLAB you are using and what the
pre designated settings are):

• 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
itʼll be easier to lay it out. Below is a sample of what the finished GUI
might look like.

3
4
Creating the Visual Aspect of the GUI
•For the adder GUI, we will need the following components

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 :

5
•Next, its time to edit the properties of these components. Letʼs 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 allows you to modify the properties of a component.

•Weʼre interested in changing the String parameter. Go ahead and


edit this text to +.

6
Letʼs also change the font size from 8 to 20.

7
For the final Static Text component, we want to set the String
Parameter to 0. In addition, we want to modify the Tag parameter for
this component. The Tag parameter is basically the variable name of
this component. Letʼs call it answer_staticText. This component will
be used to display our answer, as you have probably already have
guessed.

8
So now, you should have something that looks like the following:

•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.

9
•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.
3. 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.

10
•So now, you should have something like this:

11
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 donʼt have to worry about any of the other function
types.
•Open up the .m file that was automatically generated when you
saved your GUI.
In the MATLAB editor, click on the icon, which will bring up a list
of the functions within the .m file. Select
input1_editText_Callback.

12
Here is the code that we will add to this callback:
a = get(handles.input1_editText,'String');
b = get(handles.input2_editText,'String');
% a and b are variables of Strings type, and need to be converted
% to variables of Number type before they can be added together
total = str2num(a) + str2num(b);
c = num2str(total);
% need to convert the answer back into String type to display it
set(handles.answer_staticText,'String',c);
guidata(hObject, handles);

•Launching the GUI


1. There are two ways to launch your GUI.
The first way is through the GUIDE editor. Simply press the icon on
the GUIDE editor as shown in the figure below:

13
•The second method is to launch the GUI from the MATLAB command
prompt. First, set the MATLAB current directory to wherever you saved
your .fig and .m file.
Next, type in the name of the GUI at the command prompt (you donʼt
need to type the .fig or .m extension):

14
The GUI should start running immediately:

15

You might also like