• No results found

There are several ways to display the value of a variable. The simplest way is to enter the name of the variable. The name of the variable will be repeated, and the value of the variable will be printed starting with the next line. There are several commands that can be used to display variables with more control over the form of the display.

Number Display Options

Matlab follows several rules in displaying numerical results.

• Integers: Displayed as integers, as long as they contain 9 digits or less. For 10 digits or more, they are displayed in scientific notation, described below. For example:

>> x = 5 x =

5

• Short fixed point: The default is to display as a decimal number with four digits to the right of the decimal point, and up to three digits to the left of the decimal point, if this is possible. This is known as the short format. For example:

>> 610./12.

ans = 50.8333

• Short floating point: When the result has more than three digits to the left of the decimal point, it is displayed in scientific notation (called the short e format). Scientific notation expresses a value as a number between 1 and 10 multiplied by a power of 10. An example of the short e format, which shows four digits to the right of the decimal point:

>> 61000./12.

ans =

5.0833e+003

In this format, 5.08333 is called the mantissa, e means exponent, which has the value +003.

Written in the more common scientific notation, the result is 5.0833 × 103.

The default behavior can be changed by specifying a different numerical format within the Prefer-ences menu item in the File menu, or by typing a FORMAT command at the command prompt.

The table below indicates the affect of changing the display format for the variable ratio, computed as:

>> ratio = 610./12 ratio =

50.8333

Matlab Command ratio Comments

format short 50.8333 4 decimal digits

format long 50.83333333333334 14 decimal digits

format short e 5.0833e+001 4 decimal digits plus exponent format long e 5.083333333333334e+001 14 decimal digits plus exponent format short g 50.8333 better of format short or format

short e (default), switching for ans

> 1000

format long g 5.083333333333334e+001 better of format long or format long e

format bank 50.83 2 decimal digits

format + + positive, negative, or zero

The bank format is a fixed format for dollars and cents. The + format displays the symbols +,−, and blank for positive, negative, and zero results, respectively.

Note:

• The display formats do not change the internal representation of a number; only the display changes.

• The internal representation is rounded when the display is shortened.

For example:

>> format long

>> 5.78/57.13 ans =

0.10117276387187

>> format short

>> ans ans =

0.1012

Thus, the short result is displayed as 0.1012 instead of 0.1011, which would have been the result if the long display had been truncated to four decimal digits.

format compact suppresses many of the line feeds that appear between displays and allows more lines of information to be seen together on the screen. For example:

>> temp = 78 temp =

78

>> format compact

>> temp=78 temp =

78

In the examples shown in these notes, it is be asseumed that this command has been executed.

The command format loose can be used to return to the less compact display mode.

Displaying Values and Text

There are three ways to display values and text inMatlab, to be described in this section:

1. By entering the variable name at theMatlab prompt, without a semicolon.

2. By use of the command disp.

3. By use of the command fprintf.

• From the prompt:

As demonstrated in previous examples, by entering a variable name, an assignment statement, or an expression at the Matlab prompt, without a semicolon, the result will be displayed, proceeded by the variable name (or by ans if only an expression was entered). For example:

>> temp = 78 temp =

78

• disp:

There are two general forms of the command disp that are useful in displaying results and annotating them with units or other information:

1. disp(variable): Displays value of variable without displaying the variable name.

2. disp(string): Displays string by stripping off the single quotes and echoing the charac-ters between the quotes.

String: A group of keyboard characters enclosed in single quote marks (’). The quote marks indicate that the enclosed characters are to represent ASCII text.

>> temp=78;

>> disp(temp); disp(’degrees F’) 78

degrees F

Note that the two disp commands were entered on the same line so that they would be executed together.

• fprintf

One of the weaknesses ofMatlab is its lack of good facilities for formatting output for display or printing. A function providing some of the needed capability is fprintf. This function is similar to the function of the same name in the ANSI C language, so if you are familiar with C, you will be familiar with this command. The fprintf function provides more control over the display than is provided with disp. In addition to providing the display of variable values and text, it can be used to control the format to be used in the display, including the specification to skip to a new line. The general form of this command is:

fprintf(’format string’, list of variables)

The format string contains the text to be displayed (in the form of a character string enclosed in single quotes) and it may also contain format specifiers to control how the variables listed are embedded in the format string. The format specifiers include:

w.d%f Display as fixed point or decimal notation (defaults to short), with a width of w characters (including the decimal point and possible minus sign, with d decimal places. Spaces are filled in from the left if necessary. Set d to 0 if you don’t want any decimal places, for example %5.0f. Include leading zeros if you want leading zeroes in the display, for example %06.0f.

w.d%e Display using scientific notation (defaults to short e), with a width of w characters (including the decimal point, a possible minus sign, and five for the exponent), with d digits in the mantissa after the decimal point. The mantissa is always adjusted to be less than 1.

w.d%g Display using the shorter of tt short or short e format, with width w and d decimal places.

\n Newline (skip to beginning of next line)

The w.d width specifiers are optional. If they are left out, default values are used.

Examples:

>> fprintf(’The temperature is %f degrees F \n’, temp) The temperature is 78.000000 degrees F

>> fprintf(’The temperature is %4.1f degrees F \n’, temp) The temperature is 78.0 degrees F

It should be noted that fprintf only displays the real part of a complex number, which is an important data type to be discussed later in the course.