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.
This line space can be removed using “compact command” which is used preceded by format.
Furthermore, to bring the line spacing back in the output then, “loose command” preceded by format as shown below.
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.
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.
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).
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.
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.
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
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.
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.
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.