Matlab Program Control

Matlab uses decision and loop structures to control program execution.  The constructs are similar to VB though the actual form may be different.  The following structures are available in Matlab
     if statements
    switch statements
     for loops
     while loops
     break statements

If  Statements

There are three types :

    if    expression
           statements
      end

Here is some code

if    (A(1,1) > A(1,2) )
        A(2,1) = 2.0 * pi
end
end code

    if    expression
            statements
    else
            statements
    end

Here is some code

if     (sum(A(:,2)) > 10 )
        A(1,1) = A(1,1) + B(1,1)
        A(2,1) = pi*A(2,1)
else
        A(1,1) = A(1,1) + A(1,1);
        A(2,1) = 0.5 * pi
end
end code

    if     expression_1
            statements
    elseif    expression_2
            statements
    elseif     expression_3
            .......................

    else
            statements
    end

Here is some code

Build your own example here
end code

Switch Case statements

If you need to execute different commands for different results based on a single condition then the switch case is the control you need

switch     expression    [ can be a scalar or  a string ]
    case     test1
            command set 1
    case     test 2
            command set 2
......................................

    otherwise
            command set last
end

If expression is scalar then  case is executed if the scalar value matches the expression in the test

If expression is string then case is executed if the strings match

Here is some code

x = 3.0;
units = 'mm';

switch     units

case {'in','inch'}
    y = 2.54*x;        % converts to centimeters

case     {'m','meter'}
    y = x*100;        % converts to centimters

case { 'millimeter','mm'}
    y = x/10;
disp    ([num2str(x)  '   in   ' units ' converted to cm is :' num2str(y)])
case {'cm','centimeter'}
    y = x;

otherwise
    disp    (['unknown units:' units])
    y = nan;

end
end code

Note that the test expression in the case statements have more then one match
 

For loops

The syntax for the for loop is

for    x     =    array
           commands
end

The commands between the     for   and   end     statements are executed once for every column in array

Here is some code

for n = 1: length(A(1,:))  % default stepsize = 1
    xa(n) = A(1,n)^2;
    for m = 1:2:length(xa)    % in steps of 2
        m
        X(n,m) = n*m;        % whats happenning
                                        % how will you check whats happenning
    end
end
end code

While loops

The general form of the while loops are

while     expression
        commands
end
 

Here is some code

count = 1; num = 2;
while count < 16
    cnum= [count num];
    disp(cnum)
    num = num*2;
    count = count + 1;
end
end code

Break statements

Break statements allow you to exit for and while loop prematurely

Here is some code

count = 1; num = 2;
while count < 16
    cnum= [count num];
    disp(cnum)
    num = num*2;
    count = count + 1;
    if count >= 10
        break
    end
end
end code