Figures made more Informative – Matlab

Figures in Matlab can be used very effectively. From labeling of the axes to adding a title, adding various colours, to inserting a legend, and further adding text within the figure, or drawing lines to demarcate regions, etc. This is a long post, as I kept finding new things to add that Matlab could do.

Note that, there are a variety of plots, 3D and 2D available in Matlab. But the information provided below is more of a generalized one, pertaining and applicable to the Figure Window instead of the plot / imagesc / surf, which just inserts the data in this Window.

Subplots Annotated

Subplots Annotated

In the above image, we see an extremely efficient way in which comparison of various plots can be carried out. This is very useful when trying to compare and learn or see how the original signal gets modified over operations, step by step. This is achieved by using

subplot(221); plot(safft);
title('Sa'); xlabel('Frequency in Hz'); text(2000,19*max(safft)/20,'Harmonics 286Hz','HorizontalAlignment','center','FontSize',10);

The subplot(221) indicates that I want 2 rows, 2 columns and this is the first plot. Matlab follows a row-major filling order, thus the subsequent plots would be subplot(222) to the right, subplot(223) below and subplot(224) at the right bottom.

Further we give this plot a special title, and label the X-axis. We also insert a text using a generic form text(x-coordinate,y-coordinate,actual text,property,value) in the way shown above. We will come to this insertion of text part once again later.

Grid and Legend Example

Grid and Legend Example

You can also switch ON the grid which makes it easy to have a rough estimate of the values, and use a legend which allows you to distinguish between multiple plots on the same figure window. Note that, you can use different colours and different symbols to plot multiple items and thus show variation. The code used in this case was

hold on;
– ‘r’ to indicate red
– ‘b’ to indicate blue
grid on

hold on is used to plot multiple plots on the same figure. Also note that it automatically assigns the Legend colours to the labels respectively, i.e. the first plot which appears on the figure will correspond to the first label used. grid on does what it reads.

Annotated Figure using Matlab

Annotated Figure using Matlab - Courtesy: Ohil's TIMIT Reader

Above, is another example of a figure, (plot) in Matlab which is made extremely informative by adding what the spoken text is, and by drawing lines to demarcate word regions.

When multiple text entries are to be made, one can stack them up in a vector, and also stack the required X and Y coordinates. Thus you would say text(X-coordinate-vector,Y-coordinate-vector,Actual Text-vector,Properties) which would insert the text all over (in the specified places) the Figure window. Note that the center of the text will be at the specified coordinate.

The drawing of multiple lines is accomplished by a similar approach. The code is line(x-coordinates-mat,y-coordinates-mat,properties). Here x-coordinates-mat are the positions you want the line to appear, i.e. say at [2340, 5848, 10160, 11880, ...] and y-coordinates-mat are the entire y-dimension, from the bottom most value, to the topmost. In such cases its better to specify the xlim and ylim first, which are indicate the range of coordinates to be shown on the figure.

In reality, the x-coordinates-mat is a matrix with the rows being repeated. The rows are repeated depending on the number of parts you make of your ylim [length(-1:0.01:1) in case you set ylim(-1,1) and you intend to put a point every 0.01th coordinate]. The y-coordinates-mat is a matrix too with each column consisting of the y-coordinate points (for eg. from -1 to 1 in some stepsize) at which the line is drawn. This is repeated across columns.

This does seem a little complicated, but have a look at the examples in the line function in the Matlab help.

So these figures, nicely edited, can then be directly saved (lots of standard image formats supported) and inserted in your reports. There is no need to specially to modify them again while presenting as all the required information is on the figure itself.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s