Controlling Command Window Input and Output

March 19th, 2010 Posted in Documents

The Function

The format function controls the numeric format of the values displayed. The function affects only how numbers are displayed, not how MATLAB software computes or saves them. Here are the different formats, together with the resulting output produced from a vector x with components of different magnitudes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
x = [4/3 1.2345e-6]
 
format short
 
   1.3333    0.0000
 
format short e
 
   1.3333e+000  1.2345e-006
 
format short g
 
   1.3333  1.2345e-006
 
format long
 
   1.33333333333333   0.00000123450000
 
format long e
 
   1.333333333333333e+000    1.234500000000000e-006
 
format long g
 
   1.33333333333333               1.2345e-006
 
format bank
 
   1.33          0.00
 
format rat
 
   4/3          1/810045
 
format hex
 
   3ff5555555555555   3eb4b6231abfd271

If the largest element of a matrix is larger than 103 or smaller than 10-3, MATLAB applies a common scale factor for the short and long formats.
In addition to the format functions shown above

1
format compact

suppresses many of the blank lines that appear in the output. This lets you view more information on a screen or window. If you want more control over the output format, use the sprintf and fprintf functions.

Suppressing Output

If you simply type a statement and press or Enter, MATLAB automatically displays the results on screen. However, if you the line with a semicolon, MATLAB performs the computation but does not display any output. This is particularly useful when you generate large matrices. For example,

A = magic(100);

Entering Long Statements

If a statement does not fit on one line, use an ellipsis (three periods), …, followed by Return or Enter to indicate that the statement continues on the next line. For example,

s = 1 -1/2 + 1/3 -1/4 + 1/5 – 1/6 + 1/7 …
– 1/8 + 1/9 – 1/10 + 1/11 – 1/12;
Blank spaces around the =, +, and – signs are optional, but they improve readability.

Command Line Editing

Various arrow and control keys on your keyboard allow you to recall, , and reuse statements you have typed earlier. For example, suppose you mistakenly enter

1
rho = (1 + sqt(5))/2

You have misspelled sqrt. MATLAB responds with
Undefined function or variable ‘sqt’.
Instead of retyping the entire line, simply press the up key. The statement you typed is redisplayed. Use the left key to move the cursor over and insert the missing r. Repeated use of the up key recalls earlier lines. Typing a few characters and then the up key finds a previous line that begins with those characters. You can also copy previously executed statements from the Command History. For more information, see Command History

Leave a Reply

You must be logged in to post a comment.