Categories
2. After the Advance

Matlab Figure

MATLAB provides us with plenty of functionalities, useful in various computational problems. In addition to its computational capabilities, MATLAB is also a great tool for visualization. It provides us with the ability to plot a wide variety of charts. Apart from showing graphical output in the console, MATLAB can also have our graphical output displayed in a separate window. For achieving this, we need to create a ‘figure object’ in MATLAB using figure function, which we will learn in this article.

Figure function, MATLAB

For creating the figure object, MATLAB creates a separate window. The characteristics of this new window can be controlled using figure properties specified as arguments (Please refer to the end of the article for the custom properties).

Table for Custom Properties

here is the table for custom property:

PropertyDescriptionValue/s
ColorTo set the background colorPre-defined color code
Default: As per the color scheme
MenuBarTo toggle the menu bar on/offnone, figure
Default: figure
NameTo set the titlestring
Default: ” (empty string)
number titleTo display the figure numberon, off
Default: on
ResizeFor resizing the figure window using the mouseon, off
Default: on
SelectionHighlightTo highlight the figureon, off
Default: on
VisibleMakes figure visible/invisibleon, off
Default: on
WindowStyleNormal/modal windownormal, modalDefault: normal

Syntax:

figure (Name, Value)

We will first understand figure function in its simplest form where we will create a figure & specify the property “Name”.For this example, we will pass “Name” property as ‘Learning figure function’

Examples to Implement Matlab Figure

Here are some examples mentioned:

Example 1:

This is how our input and output will look like in MATLAB console:

Code:

figure ('Name', 'Learning figure function')

Output:

Matlab Figure1

Explanation: As we can observe in the output obtained, we have obtained a new window as a figure object and our figure’s name is as passed by us “Learning figure function”. Also, notice ‘Figure 1’ before the name of the figure, this is done by MATLAB as a default property. However, we can get rid of this.

Example 2:

Let us learn how to get the name without the figure number. To achieve this, we need to keep the ‘Numbertitle’ property of figure function as ‘off’. This is how our input and output will look like in MATLAB console:

Code:

figure('Name', 'Learning figure function','NumberTitle','off');

Output:

Matlab Figure2

Note: In the output that we have obtained a new window without the figure number.

Example 3:

Next we will learn how we can get our graph in the figure object.  To get the graph in a new window, we first create the figure object as above and then write the syntax to create the desired plot. MATLAB by default assigns the plot to the latest figure object created.

In our example, we will create a bar plot in the figure object.

X = [12, 20, 13, 40, 40, 23, 54, 65, 11, 40, 70, 45, 60, 33][Input array to create bar plot]

This is how our input and output will look like in MATLAB console:

Code:

X = [12, 20, 13, 40, 40, 23, 54, 65, 11, 40, 70, 45, 60, 33] figure ('Name', 'Learning figure function','NumberTitle','off')
bar (X)

Output:

Matlab Figure3

Example 4:

As we can see, our output bar chart is in a new window and with the name as passed by us. We can also change the background of the new window using the ‘Color’ property of figure function. Let us learn how to do that.

For our example, we will set the ‘Color’ property to ‘c’ which is pre-defined color code for the color cyan.Note: In the above line of code that we have set the ‘Color’ property as ‘c’ to get Cyan color

This is how our input and output will look like in MATLAB console:

Code:

X = [12, 20, 13, 40, 40, 23, 54, 65, 11, 40, 70, 45, 60, 33] figure ('Name', 'Learning figure function','NumberTitle','off', 'Color', 'c')
bar (X)

Output:

background Color

Explanation: So, we have our output window in CYAN color now.

Example #5

Let us take another example where we will create a scatter plot in a new window using figure function. For this example, we will get our background in Red color.

scatter((1:40),and(1,40)); [Creating scatter plot using random values]

As explained earlier, MATLAB will by default plot the graph in the figure object created. This is how our input and output will look like in MATLAB console:

Code:

figure ('Name', 'Learning figure function', 'NumberTitle', 'off', 'Color','r')
scatter((1:40),rand(1,40));

Output:

scatter plot

Explanation: As we can see in our output, we have our scatter plot created in a new window, with red color.

Leave a Reply

Your email address will not be published. Required fields are marked *