# A Guide to MATLAB for Beginners and Experienced Users - Brian R.H.

**Download**(direct link)

**:**

**35**> 36 37 38 39 40 41 .. 91 >> Next

A more insidious problem is the following. If you cut and paste character strings into an input cell, the characters in the original font may be converted into something you don't anticipate in the Courier input cell. Mysterious and unfathomable error messages upon execution are a tip-off to this problem. In general, you should not copy cells for evaluation unless it is from a cell that has already been evaluated successfully — it is safer to type in the line anew.

Finally, we have seen instances in which a cell, for no discernible reason, fails to evaluate. If this happens, try typing ctrl+enter again. If that fails, you may have to delete and retype the cell. We have also occasionally experienced the following problem: Reevaluation of a cell causes its output to appear in an unpredictable place elsewhere in the M-book — sometimes even obliterating unrelated output in that locale. If that happens, click on the Undo button on the Word tool bar, retype the input cell before evaluating, and delete the old input cell.Chapter 7

MATLAB Programming

Every time you create an M-file, you are writing a computer program using the MATLAB programming language. You can do quite a lot in MATLAB using no more than the most basic programming techniques that we have already introduced. In particular, we discussed simple loops (using for) and a rudimentary approach to debugging in Chapter 3. In this chapter, we will cover some further programming commands and techniques that are useful for attacking more complicated problems with MATLAB. If you are already familiar with another programming language, much of this material will be quite easy for you to pick up!

? Many MATLAB commands are themselves M-files, which you can examine using type or edit (for example, enter type isprime to see the M-file for the command isprime). You can learn a lot about MATLAB programming techniques by inspecting the built-in M-files.

Branching

For many user-defined functions, you can use a function M-file that executes the same sequence of commands for each input. However, one often wants a function to perform a different sequence of commands in different cases, depending on the input. You can accomplish this with a branching command, and as in many other programming languages, branching in MATLAB is usually done with the command if, which we will discuss now. Later we will describe the other main branching command, switch.

101102

Chapter 7: MATLAB Programming

Branching with if

For a simple illustration of branching with if, consider the following function M-file absval.m, which computes the absolute value of a real number:

function y = absval(x) if x >= 0 y = x;

else

Ó = -x;

end

The first line of this M-file states that the function has a single input x and a single output y. If the input x is nonnegative, the if statement is determined by MATLAB to be true. Then the command between the if and the else statements is executed to set y equal to x, while MATLAB skips the command between the else and end statements. However, if x is negative, then MATLAB skips to the else statement and executes the succeeding command, setting y equal to -x. As with a for loop, the indentation of commands above is optional; it is helpful to the human reader and is done automatically by MATLAB's built-in Editor/Debugger.

? Most of the examples in this chapter will give peculiar results if their input is of a different type than intended. The M-file absval.m is designed only for scalar real inputs x, not for complex numbers or vectors. If x is complex for instance, then x >= 0 checks only if the real part of x is nonnegative, and the output y will be complex in either case. MATLAB has a built-in function abs that works correctly for vectors of complex numbers.

In general, if must be followed on the same line by an expression that MATLAB will test to be true or false; see the section below on Logical Expressions for a discussion of allowable expressions and how they are evaluated. After some intervening commands, there must be (as with for) a corresponding end statement. In between, there may be one or more elseif statements (see below) and/or an else statement (as above). If the test is true, MATLAB executes all commands between the if statement and the first elseif, else, or end statement and then skips all other commands until after the end statement. If the test is false, MATLAB skips to the first elseif, else, or end statement and proceeds from there, making a new test in the case of an elseif statement. In the example below, we reformulate absval.m so that no commands are necessary if the test is false, eliminating the need for an else statement.Branching 103

function y = absval(x) y = x; if y < 0

y = -y;

end

The elseif statement is useful if there are more than two alternatives and they can be distinguished by a sequence of true/false tests. It is essentially equivalent to an else statement followed immediately by a nested if statement. In the example below, we use elseif in an M-file signum.m, which evaluates the mathematical function

**35**> 36 37 38 39 40 41 .. 91 >> Next