在MATLAB

时间:2017-06-29 12:38:12

标签: matlab plot matlab-figure

我在MATLAB中使用以下脚本生成带有许多波特图功能的图:

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

现在我将句柄存储在h变量中。如何使用处理程序以编程方式更改每个绘图的线条颜色和样式?

例如,我想将位置(1,2)的绿色相位图从绿色更改为红色,将位置(2,1)的蓝色幅度更改为黑色。

2 个答案:

答案 0 :(得分:1)

使用传统的图形手柄方式似乎更方便。

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

bodeplots的子项以相反的顺序编入索引,即fh.Children按以下顺序编号:

image1
fh.Children的顺序,1是ContextMenu

此刻忽略上图中的线条颜色(稍后会讨论)。

然后更进一步,fh.Children的孩子被命令使1成为第一个系统,2成为第二个系统,依此类推。

线条的颜色属性实际上是上述孩子的属性。

实施例

对于MATLAB R2014a及更早版本(也适用于更高版本):

查看您的bodeplot的color of lines,您似乎正在使用MATLAB R2014a或更早版本,因此您必须使用getset

  1. 更改第1行和第1行中的绿色相位图。第二列为红色,使用:

    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. 同样改变第4行中的蓝色幅度图&第1列为黑色,使用:

    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');
    
  3. 对于MATLAB R2014b及更高版本:

    1. 要更改第1行和第2行中的绿色(R2014b及更高版本中的橙色)相位图。第二列为红色,使用:

      fh.Children(5).Children(2).Children.Color = 'r';     
      % '5' according to the map shown before. And '2' since it is the second system 
      
    2. 同样改变第4行中的蓝色幅度图&第1列为黑色,使用:

      fh.Children(6).Children(1).Children.Color = 'k';
      % '6' according to the map shown before. And '1' since it is the first system 
      
    3. documentation中查看颜色的短/长名称及其RGB三元组,或使用其RGB值指定您自己的颜色。其他行属性(例如LineStyleLineWidthMarkerSize等也可以以类似的方式更改。

      也可以使用getoptionssetoptions使用bodeplot句柄。

答案 1 :(得分:0)

您可以从绘图中提取线条手柄并通过

更改单个颜色
 linehandle = findobj(gcf, 'Type','line');
 set(linehandle(ii), 'Color', [1 1 0]);

问题是将一条线映射到一个特定的图(搜索查询找到24行)。

分析相应轴的位置可能会有所帮助。例如

 linehandle(9).Parent.Parent

递送

 Axes with properties:

           XLim: [0.0100 100]
           YLim: [-363.6000 3.6000]
         XScale: 'log'
         YScale: 'linear'
  GridLineStyle: '-'
       Position: [0.5425 0.1100 0.3625 0.1580]
          Units: 'normalized'

这可能是右下图。对象搜索也可以按逻辑条件进行过滤:

 lineHandle = findobj(gcf,'Type','line','-and','Color','b');

但是我担心为特定的功能图分配一个行句柄最终会尝试错误...

相关问题