## SUMMARY

1. Using disp() to avoid "A = ".

disp(A) gives A without the statement, " A = "
disp(' A is a matrix ') gives the statement typed between the apostrophes.
Use disp(' ') to skip a line.

2. The number of decimal places in the output.

For 14-15 places:

format long 57.27564927611034

format short 57.2756

Floating point (scientific) notation:

format short e 5.7276e+001
format long e 5.727564927611034e+001

Converting matrices to the + { } - format

format +

[Note: Recent versions of MATLAB have more commands for specifying the format: e.g.

format bank 57.27 (2 decimal places, for money!)

sprintf This command is based on C syntax and can be used to control the number of decimal places more precisely. String statements (such as in disp('hello ') ) can also be included:

a = 57.27564927611034
sprintf( ' %10.5f ' , a ) = 57.27564
[5 decimal places; the 10 indicates the printing is being done in a minimum of 10 spaces with the "4" being placed on the extreme right.]

Using e instead of f will give the answer in floating point notation.

format hex the output will be in hexadecimal format.

type help format, help fprintf for details of what is available on the system that you are using. ]

# DETAILS AND EXAMPLES

1. Using disp( ) to avoid "A = "; construction of simple tables.

MATLAB output is usually in the form: "A = " followed by the value of A. To print out just the value of A we use disp( ) :

>> disp(A)

The display command is also used to produce alphanumeric text. The text to be displayed is enclosed within apostrophes. Thus if we enter:

>> disp(' A is a matrix ')

we obtain:

A is a matrix

The use of disp( ) is illustrated in the following two methods of producing a table of sines and cosines. There are various ways of producing the individual results and these are also illustrated. In the first method we produce individual row vectors of values one at a time and print them out; this method is shown first in step by step format and then in a program. In the second method, we compute the vectors, take their transposes and create a matrix whose columns are these transposes. We then ask MATLAB to display the matrix.

Example 1. Producing the output in rows.

We first form a vector "values" that gives the range of values that interests us.

2.  >> values =[0 pi/4 pi/2]; % ; to suppress printing >> disp(values) 0 0.7854 1.5708

Next, we calculate the sines of the values in "values" by using the MATLAB vector function sin( ). MATLAB trigonometric and other functions are discussed in detail in section II.3. We use both sin( ) and disp( ) in the same statement

>> disp( sin( values ) )

0 0.7071 1.0000

For the sine, we did not assign a symbol, but we do so for the cosine values.

[Warning! A common error is to use a function name for a variable, e.g to let cos = cos(values). Note that we have used the underbar in the variable name "cos_variables", because the use of a minus sign, e.g. "cos-values" would make MATLAB think that we want to subtract! ]

>> cos_values = cos(values);
>> disp(cos_values)

1.0000 0.7071 0.0000

Finally we produce a title for our table. To indicate that we are dealing with an alphanumeric quantity we enclose it in apostrophes. Some space is left after the first comma to shift the title over to the right:

>> label = ' table of sines and cosines';
>> disp(label)

table of sines and cosines % MATLAB response

Above, the commands were given on different lines, but they could also have been put on one line using the comma. Since the line is too long we use ellipses to indicate that we want to continue on the next line, although in this case it is just as simple to push ENTER and go to the next command line. Note how we use disp(' '), i.e. the message is empty, in order to have MATLAB skip a line

>> disp(label) , disp(values) , disp(sin( values )) , ... disp(' ') , disp(cos_values)

table of sines and cosines

 0 0.7854 1.5708 0 0.7071 1 1 0.7071 0

Instead of doing the above steps individually we can write a short program "disp2.m".

# Program 1

 % [disp2.m] x = [0:.2:1]; % ; to suppress printing disp(' first row values; second is sine; third is cosine') disp(' ') % empty message to leave a space disp(x) disp( sin(x) ) disp( cos(X) )

We now call up "disp2.m". Note that we just type "disp2" and not "disp2.m"

>> disp2

The first row is values; second is sine; third is cosine

 0 0.2 0.4 0.6 0.8 1 0 0.1987 0.3894 0.5646 0.7174 0.8415 1 0.9801 0.9211 0.8253 0.6967 0.5403

Example 2. Producing the output in columns.

In the first method, the output is in rows, but if the range of values is above nine or ten, then the rows will not fit on one line of print. So, instead of working exclusively with row vectors we take the transpose of the vectors, create a matrix whose columns are these vectors and then print the matrix. This method is followed in the program "disp3.m". Note how we compute the variables, but do not print them immediately. It is sometimes difficult to place the title exactly and it is often simpler to make a final adjustment with a text editor. Since the entries are all real, we can use the operator ' for transpose instead of .' (see section II.1). In the program we first use the semicolon (section I.2.v) to generate a domain vector with values going form 0 to 1 by steps of .2.

# Program 2

 % [disp3.m] y = ( [0 : .2 : 1] )'; % ' change to a column vector si = sin(y); % ; to suppress printing co = cos(y); A = [y , si , co]; % put in a matrix by means of `,' disp(' x sin(x) cos(x) ') % display the title disp(' ') % empty message, leaves a space disp(A) % display the matrix

We now call the program:

 >> dispro3 % type filename - without the extension .m

 x sin(x) cos(x) 0 0 1.0000 0.2000 0.1987 0.9801 0.4000 0.3894 0.9211 0.6000 0.5646 0.8253 0.8000 0.7174 0.6967 1.0000 0.8415 0.5403

One should not spend too much time lining up a title inside the disp( ' ' ); it is usually simpler to do it later with a text editor.

3. The number of decimal places in the output.

MATLAB usually shows the results to five digits; to display more places use format long:

>> s1 = 81*sin(pi/4)

57.2756

>> format long

 >> s1 % type s1 to display it

57.27564927611034

To display the answer in "floating point" notation use format short e or format long e

>> format short e
>> s1

5.7276e+001

>> format long e
>> s1

5.727564927611034e+001

>> format short
>> s1

57.2756

For some matrices we may want to just see if elements are negative, zero or positive. Use the "format +" command. MATLAB leaves a very small space where the terms are 0.

>> format +
>> C = [-2 0 5]

 - + % There is a space between the - and +

Top 