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

(Steven Felgate) #1
formatting characters, such as \nand \t, to produce linefeed and tabs. The following example
will demonstrate the use of the fprintfcommand.

Example 15.1 In the MATLAB Command Window, type the following commands as shown.


x=10
fprintf('The value of x is %g \n', x)

The MATLAB will display


The value of x is 10


A screen capture of the Command Window for Example 15.1 is shown in Figure 15.3.


466 Chapter 15 MATLAB


■Figure 15.3
The screen capture of
the Command Window
for Example 15.1.

Note that the text and formatting code are enclosed within a set of single quotation marks.
Also note that the %g is the number format character and is replaced by x value, 10. It is also
important to mention that MATLAB will not produce an output until it encounters the \n.
Additional capabilities of dispand fprintfcommands will be demonstrated using
other examples later.

Saving Your MATLAB Workspace


You can save the workspace to a file by issuing the command: save your_filename. The
your_filename is the name that you would like to assign to the workspace. Later, you can load the
file from the disk to memory by issuing the command: load your_filename. Over time
you may create many files; then you can use the dircommand to list the content of the direc-
tory. For simple operations you can use the Command Window to enter variables and issue MAT-
LAB commands. However, when you write a program that is longer than a few lines, you use an
M-file. Later in this chapter, we will explain how to create, edit, run, and debug an M-file.

Generating a Range of Values


When creating, analyzing, or plotting data, it is often convenient to create a range of numbers.
To create arangeof data or a row matrix, you only need to specify the starting number, the in-
crement, and the end number. For example, to generate a set ofxvalues in the range of zero to
100 in increments of 25 (i.e., 0 25 50 75 100), in the Command Window, type

x = 0:25:100


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