Department of Engineering

IT Services

Suppressing and controlling output

Matlab is frequently used with vectors and matrices containing hundreds or thousands of entries. In such cases it is a great inconvenience to have the answers written to the terminal. It is easy, however, to prevent Matlab from writing the answer to any calculation: simply terminate a statement by a semi-colon (;) and Matlab will execute it without writing out the result. For example, generate a vector of 100 evenly spaced points between 0 and π (remember the semi-colon, and distinguish it from the colons (:) which are used to define the vector conveniently):

t = linspace(0, pi, 100);

Now you can examine the first 10 values, say, by typing t(1:10) (without a semi-colon). If you just type t you will get all 100 points. We can also make a vector of the values of sin(t) for 0<= t <= π

 
sint = sin(t);

Did you remember the semi-colon?

In engineering we often need to use `scientific' notation, in which numbers are written in the `mantissa x exponent' form x.xxxx x 10x. We can make Matlab output results in this form. First let's look at the values in the vector sint near to t=π/2 say the 49th, 50th, 51st and 52nd entries:

 
sint(49:52)

Now switch to scientific notation by typing

 
format short e

and look at those values again. You will see the values to 4 decimal places, with powers of 10 (labelled `e' for exponent). If you would like to see the values to the full accuracy with which they are stored in Matlab, type

 
format long e

and look at them again. If you would like to restore the original format, type

 
format

on its own.