Engineering Fundamentals: An Introduction to Engineering, 4th ed.c

(Steven Felgate) #1
Note that in MATLAB, the elements of the matrix are enclosed in brackets [ ]and are separated
by blank space, and the elements of each row are separated by a semicolon ;as shown in Figure 15.2.
In Section 18.5, we will discuss matrix algebra in more detail. If you do not have an ade-
quate background in matrix algebra, you may want to read Section 18.5 before studying the
MATLAB examples that deal with matrices.

The format,disp,and fprintfCommands


MATLAB offers a number of commands that you can use to display the results of your calcu-
lation. The MATLAB formatcommand allows you to display values in certain ways. For
example, if you define x = 2/3, then MATLAB will display x = 0.6667. By default,
MATLAB will display four decimal digits. If you want more decimal digits displayed, then you
type format long. Now, if you type xagain, the value ofxis displayed with 14 decimal
digits, that is x = 0.66666666666667. You can control the way the value ofxis displayed
in a number of other ways, as shown in Table 15.2. It is important to note that the format
command does not affect the number of digits maintained when calculations are carried out by
MATLAB. It only affects the way the values are displayed.
The dispcommand is used to display text or values. For example, given x = [1 2 3
45],then the commanddisp(x)will display 12345. Or the commanddisp
('Result = ')will display Result =. Note that the text that you want displayed must
be enclosed within a set of single quotation marks.
The fprintfis a print (display) command that offers a great deal of flexibility. You can
use it to print text and /or values with a desired number of digits. You can also use special

15.1 MATLAB —Basic Ideas 465


TABLE 15.2 The Format Command Options


How the Result of
MATLAB Command x 2/3 is Displayed Explanation

format short 0.6667 Shows four decimal
digits-default format.

format long 0.66666666666667 Shows 14 decimal digits.


format rat 2/3 Shows as fractions of whole
numbers.

format bank 0.67 Shows two decimal digits.


format short e 6.6667e-001 Shows scientific notation
with four decimal digits.

format long e 6.666666666666666e-001 Shows scientific notation
with 14 decimal digits.

format hex 3fe5555555555555 Shows hexadecimal output.


format +  Shows , , or blank based
on whether the number is
positive, negative, or zero.

format compact It suppresses the blank lines
in the output.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀圀圀圀⸀夀䄀娀䐀䄀一倀刀䔀匀匀⸀䌀伀䴀

Free download pdf