Formatting output and help in MATLAB

By Soumya Srivastava on September 2, 2017

In the previous articles, the discussion revolved around the use of command window and how variables are added to working window. In the present article, the focus will be on how to format output and further use MATLAB for documentation without errors.

By default, MATLAB displays a line space between different lines in the output.

Line Spacing in Output
Line Spacing in Output

This line space can be removed using “compact command” which is used preceded by format.

Use of Compact Command
Use of Compact Command

Furthermore, to bring the line spacing back in the output then, “loose command” preceded by format as shown below.

Use of loose command
Use of loose command

Formatting floating point numbers

Floating point are the numbers wherein the number of digits before and after the decimal is not fixed that is the decimal is floating. By default, MATLAB uses the command “short” to present output which has a fixed decimal with four digits after decimal. Image shows how the value of pi is presented in short format with four digits after decimal.

Formatting floating point number using short
Formatting floating point number using short

The format of the result presented in the output can be changed using “format command” succeeded by the type to be used (long in case of present example). Image shows the presentation of value of pi in long format.

Formatting floating point number using long
Formatting floating point number using long

The above image presents the generation of a 2 by 2 random matrix (wherein, Function rand ( ) is used to generate a matrix of random numbers) in case of “longEng” format. Furthermore, the presentation can be changed to the default short format any time by using format command (Image 6).

Formatting floating point numbers using "longEng" and default format
Formatting floating point numbers using “longEng” and default format

Similarly, there are many other formats are present for formatting output. Some of which are longE, shortG, long, shortEng, longEng, bank, hex and rat.

View output page by page

Many a times, the output is so big that it exceeds the visible area in the command window. In such a scenario, to view the output screen by screen, “more on” command is used. Type more on command in the command window followed by a code that will generate a bigger output. Image represents an example to illustrate the more command.  Herein, ‘for loop’ is used to generate the matrix a fifty time as multiple of 5.

Use of more command in MATLAB
Use of more command in MATLAB
Output generated with "more" command
Output generated with “more” command

Above output is generated, which has generated matrix for five times, forty five times is still left which will be presented in proceeding pages. On pressing enter or spacebar after more, next page of output appears as in the below.

Second page of output generated using more command
Second page of output generated using more command

In order to stop display of output, you need to type “q” after “more”. This will result in new line to carry on with other commands.

Quick tip: On typing “q” and pressing enter, the “q” disappears and new line appears as shown in Image 10

Terminating the generation of output using 'q'
Terminating the generation of output using ‘q’

Clearing the command window

Command window can be cleared without clearing the workspace window by using any of the following methods. “Home” function can be used to clear the present view of the command window without clearing the scroll buffer. Function “clc” is used to clear the command window along with scroll buffer.

Command window can also be cleared along with scroll buffer by following the path as follows:

Home Tab-> Code Section-> Clear Commands-> Command Window

Long statements on multiple lines

Another form of presentation is to reflect content in single view. In order to do so the statement can be presented on multiple lines using ellipsis as shown in the below image.

Use of ellipsis for concatenation
Use of ellipsis for concatenation

A long character vector can also be created using single codes at the start and at end of the line as shown in the below image.

Use of “single quotes” for concatenation
Use of “single quotes” for concatenation

 

The next article will present the ways in which MATLAB provides help to find content and understanding functions via internal tutorials and information in the software.

Discuss