Unit# 9- MATLAB 2
Course Outline Return to Unit
MATLAB: Control Structures- if statements
As you read this material we strongly recommend that you activate your MATLAB window and try the commands explained right there and then

The if structures in MATLAB are very similar to the ones we learned in C++.  The following  table illustrates the similarities/differences:

If structures in C++

If structures in MATLAB

Differences

if  (  x >= 0 &&  x <=1.0 )
{
y = sin ( pi * x );
cout <<”\n  y = \n “ << y;
}
else
{
y = 3.0;
cout << “\n  constant level  \n “;
}

if  ( x >= 0  &  x <= 1.0 )

y = sin ( pi * x );
fprintf ( ‘\n y = %f  \n‘, y );

else

y = 3.0;
fprintf (‘\n constant level \n‘);

end

 

We can write and with only one &

No need for brackets ( contin.  to else)

end plays the role of the bracket

The following table lists the relational, equality and logical operators in C++ and in MATLAB and it points out the few differences:

Meaning

Operators in C++

Operators in MATLAB

Differences

less than

<

<

 

less than or equal

<=

<=

 

greater than

>

>

 

greater than or equal

>=

>=

 

equal

= =

==

 

not equal

!=

~=

use ~ rather than !

and

&&

&

only one &

or

||

|

only one vertical bar

not

!

~

~ rather than !

 

MATLAB supports the multiple branching if statements which use the structure  if ญ elseif ญ elseif- …. ญ else . The following example illustrates this.  However we should point out that the word ifelse must be typed as a single word (C++   supports a separated spelling as “else if “ because the use of curly brackets clarifies the block and thus there is no need to interpret the next key word ) .

EXAMPLE:



%   ___________________________________
% Jacob Y. Kazakia jyk0
% September 28, 2001
% __________________________________
% Purpose: This program compares the car emissions for four different
% pollutants to given emissions limits. The age of the car is taken
% under consideration. An appropriate message is printed.
% Algorithm: The pollutant is selected by choosing an integer ( pol_number)
% from a menu of the form:
% (1) Carbon monoxide
% (2) Hydrocarbons
% (3) Nitrogen oxides
% (4) Non-methane hydrocarbons
% It prompts the user to enter the emission (emission) in grams
% per mile.
% It prompts the user to enter the odometer reading ( odom).
% Compares the entered value to the limits given in the
% following table ( from Joseph Priest, Energy:Principles, Problems
,
% Alternatives[ Addison-Wesley, 1991] ): % First 5 Years or Second 5 Years or
% 50,000 Miles Second 50,000 Miles
% carbon monoxide 3.4 gr/mile 4.2 gr/mile
% hydrocarbons 0.31 " " 0.39 " "
% nitrogen oxides 0.4 " " 0.5 " "
% Non-methane hydrocarbons 0.25 " " 0.31 " " fprintf('\n (1) Carbon monoxide ');
fprintf('\n (2) Hydrocarbons ');
fprintf('\n (3) Nitrogen oxides ');
fprintf('\n (4) Non-methane hydrocarbons ');
pol_number = input(' \n\n Enter a pollutant number >>> ');
emission = input(' \n\n Enter the emission in grams per mile >>> ');
odometer = input(' \n\n Enter odometer reading >>> '); if( pol_number == 1 & odometer <= 50000 & emission <= 3.4 )
fprintf(' \n\n permissible emission of carbon monoxide'); elseif ( pol_number == 1 & odometer <= 50000 & emission > 3.4 )
fprintf(' \n\n non permissible emission of carbon monoxide');

elseif ( pol_number == 1 & odometer > 50000 & emission <= 4.2 )
fprintf(' \n\n permissible emission of carbon monoxide');
elseif ( pol_number == 1 & odometer > 50000 & emission > 4.2 )
fprintf(' \n\n non permissible emission of carbon monoxide');
% Second pollutant

elseif ( pol_number == 2 & odometer <= 50000 & emission <= 0.31 )
fprintf(' \n\n permissible emission of carbon monoxide');

elseif ( pol_number == 2 & odometer <= 50000 & emission > 0.31 )
fprintf(' \n\n non permissible emission of carbon monoxide');

elseif ( pol_number == 2 & odometer > 50000 & emission <= 0.39 )
fprintf(' \n\n permissible emission of carbon monoxide');
elseif ( pol_number == 2 & odometer > 50000 & emission > 0.39 )
fprintf(' \n\n non permissible emission of carbon monoxide'); % third pollutant
elseif ( pol_number == 3 & odometer <= 50000 & emission <= 0.4 )
fprintf(' \n\n permissible emission of carbon monoxide');

elseif ( pol_number == 3 & odometer <= 50000 & emission > 0.4 )
fprintf(' \n\n non permissible emission of carbon monoxide');

elseif ( pol_number == 3 & odometer > 50000 & emission <= 0.5 )
fprintf(' \n\n permissible emission of carbon monoxide');
elseif ( pol_number == 3 & odometer > 50000 & emission > 0.5 )
fprintf(' \n\n non permissible emission of carbon monoxide');
% fourth pollutant
elseif ( pol_number == 4 & odometer <= 50000 & emission <= 0.25 )
fprintf(' \n\n permissible emission of carbon monoxide');

elseif ( pol_number == 4 & odometer <= 50000 & emission > 0.25 )
fprintf(' \n\n non permissible emission of carbon monoxide'); elseif ( pol_number == 4 & odometer > 50000 & emission <= 0.31 )
fprintf(' \n\n permissible emission of carbon monoxide');
elseif ( pol_number == 4 & odometer > 50000 & emission > 0.31 )
fprintf(' \n\n non permissible emission of carbon monoxide'); % invalid entry
else

fprintf(' \n\n You entered an invalid pollutant number');
fprintf(' \n Program will terminate ');
end



( the example in text form)

MATLAB also supports switch statements.  You can find information about them by searching the HELP index for switch.

Jacob Y. Kazakia © 2001 All rights reserved