Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 11

Task 11.2 Detail: Changing label characteristics on a plot

Summary of new tools and commands.

Task: Create a simple line plot using the variables created in Task11.1. Save handles for the labels and title. Make each labels and title a different size and a different color. You might experiment with different values for Rotation to see its effect on the text in the title.

All of the labeling on a graph are Text objects. The following simple plot has 3 text objects associated with the title and the two labels.

  Hp=plot(x,y);
  Hx=xlabel('time');
  Hy=ylabel('weight');
  Ht=title('Weight vs Time');

Text objects have a large number of properties. We will work with the following three properties: FontSize, FontWeight, and Color. Not surprising, the Color property needs the name of a color (or an RGB triplet). FontSize needs a number (in points: a printer unit which is 1/72 of an inch). It is sometimes not easy to forecast the size of the resulting print. Experience is that values of 14 or 16 give large enough print to read comfortably. FontWeight can have the values 'normal', and 'bold'. There was a half-bold weight, 'demi', which is no longer available. Using this choice results in normal weight.

Helvetica is the default font used on graphs. In some circumstances, journals will specify the font to use on graphs (this is not common, but it happens). A different font family can be specified with set(Ht,'FontName','NAME'). The question is, what fonts are available. The answer may depend on the fonts installed by MATLAB or by your computer operating system. To find the installed fonts available to MATLAB, use the command listfonts.

Under some circumstances, the text needs to be aligned in some particular way. The properties HorizontalAlignment and VerticalAlignment can be specified. See the documentation for appropriate choices.

Finally, the line of text can be rotated, such as when the text is written as a label on the y axis with ylabel. Specifying the property Rotation with a number of degrees will rotate the line of text relative to the beginning point of the text line. Positive angles rotate the text counter-clockwise.

Flow chart for task:

%%%  create t and F
%%%  plot the line, title, and labels saving handles
%%%  change xlabel properties
%%%  change ylabel properties
%%%  change title properties

Solution script for the task:

%%%  create t and F
t=0:.5:10;F = t.*cos(.1*t);
figure;
%%%  plot the line, title, and labels saving handles
 Hp=plot(t,F,'r--');
 Hx=xlabel('Time(hours)');
 Hy=ylabel('Attention Span');
 Ht=title('Mental Alertness');
%%%  change xlabel properties
 set(Hx,'FontSize',14,'Color','r')
%%%  change ylabel properties
 set(Hy,'FontSize',15,'Color','b')
%%%  change title properties
 set(Ht,'FontSize',18,'Color','g','Rotation',10)

   SS11_2

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 11


email: J. Klinck