Search code examples
matlabplotmatlab-figure

Change style of a bodeplot with many plots in MATLAB


I've the following script in MATLAB that generates a plot with many bodeplot functions:

function bodetest()
  bodesample = tf([3, -2, 1], [4, -5, 5, 6, 3]);
  bodesample2 = tf([1, -1, 1], [4, 7, 5, 6, 3]);
  h = bodeplot([bodesample, bodesample; bodesample, bodesample],[bodesample2, bodesample2; bodesample2, bodesample2]);
end

enter image description here

Now I've the handle stored in h variable. How can I change the line color and style for every individual plot programmatically by using the handler?

For example I want to change the green phase plot in position (1, 2) from green to red, and the blue magnitude of the plot in position (2, 1) in black.


Solution

  • Using the conventional way of using the figure handle seems more convenient.

    %Making desired bodeplots
    fh=figure;    %Figure handle
    bodeplot([bodesample, bodesample; bodesample, bodesample], ...   %Bodeplot for System-1 
        [bodesample2, bodesample2; bodesample2, bodesample2]);       %Bodeplot for System-2
    

    The children of bodeplots are indexed in the reverse order i.e. fh.Children are in the following order:

    image1
    Fig. Order of fh.Children, 1 is the ContextMenu

    Ignore the color of lines in the above figure at the moment (it will be discussed later).

    Then going further, the children of fh.Children are ordered such that 1 being the first system, 2 being the second system and so on.

    The color property of the lines is actually property of the children of the above.


    Examples

    For MATLAB R2014a and earlier versions (also applicable on the later versions):

    Looking at the color of lines of your bodeplot, it seems that you're using MATLAB R2014a or an earlier version so you would have to use get and set.

    1. To change the green-colored phase plot in 1st row & 2nd column to red, use:

      ch=get(fh,'children');  ch=get(ch(5),'children');  ch=get(ch(2),'children'); 
      % '5' according to the map shown before. And '2' since it is the second system
      set(ch,'color','r');
      
    2. Similarly to change the blue-colored magnitude plot in 4th row & 1st column to black, use:

      ch=get(fh,'children');  ch=get(ch(6),'children');  ch=get(ch(1),'children');
      % '6' according to the map shown before. And '1' since it is the first system 
      set(ch,'color','k');
      

    For MATLAB R2014b and later versions:

    1. To change the green-colored (which is orange in R2014b and later versions) phase plot in 1st row & 2nd column to red, use:

      fh.Children(5).Children(2).Children.Color = 'r';     
      % '5' according to the map shown before. And '2' since it is the second system 
      
    2. Similarly to change the blue-colored magnitude plot in 4th row & 1st column to black, use:

      fh.Children(6).Children(1).Children.Color = 'k';
      % '6' according to the map shown before. And '1' since it is the first system 
      

    See the short/long names of colors and their RGB triplets in the documentation or specify your own color with its RGB values. Other line properties such as LineStyle, LineWidth, MarkerSize etc can also be changed in the similar way.

    It may also be possible using the bodeplot handle using getoptions and setoptions.