【问题标题】:MATLAB: Changing the line properties of a loaded figure?MATLAB:更改加载图形的线属性?
【发布时间】:2012-03-08 22:23:44
【问题描述】:

我有一个非常简单的问题,对于 MATLAB 用户:

如果我使用 load 命令加载图形文件 (.fig),是否有任何方法可以从命令行更改绘图线属性? (宽度、颜色、标记等)

PD:根据 Defining the Color of Lines for Plotting On this page… 中的信息,前两个选项仅在您使用 plot 命令时才有效。显然,如果您加载图形,它们将毫无用处。

【问题讨论】:

    标签: matlab plot edit matlab-figure


    【解决方案1】:

    为了操作图中的对象,您需要访问它们的句柄。如果您使用绘图函数创建图形,这些函数将向您返回句柄。当您打开一个图形时,就像您的情况一样,您需要按照图形对象树来查找要操作的特定元素的句柄。

    This page 有关于图形对象结构的信息。

    您想要的句柄的路径取决于您的图形,但是,例如,如果您的图形是使用简单的plot 命令创建的,这将是更改线条属性的一种方法:

    x = 0:0.1:2;
    plot(x,sin(x));
    
    fig = gcf % get a handle to the current figure
    % get handles to the children of that figure: the axes in this case
    ax = get(fig,'children') 
    % get handles to the elements in the axes: a single line plot here
    h = get(ax,'children') 
    % manipulate desired properties of the line, e.g. line width
    set(h,'LineWidth',3)
    

    【讨论】:

    • 感谢您的信息。我会考虑的。但是我发现上面的替代方案更直观。
    【解决方案2】:

    您可以使用FINDOBJ 函数获取当前图形上所有线条对象的句柄:

    hline = findobj(gcf, 'type', 'line');
    

    然后你可以改变所有线对象的一些属性:

    set(hline,'LineWidth',3)
    

    或仅针对其中一些:

    set(hline(1),'LineWidth',3) 
    set(hline(2:3),'LineStyle',':') 
    idx = [4 5];
    set(hline(idx),'Marker','*') 
    

    【讨论】:

      【解决方案3】:

      除了@yuk 回答,如果你也有图例

      hline = findobj(gcf, 'type', 'line');
      

      将返回 N x 3 行(或更准确地说 - lines plotted + 2x lines in legend)。 我这里只看所有绘制的线都在图例中的情况。

      排序很奇怪: 如果绘制了 5 行(让我们记下它们1 to 5)并添加了图例,您将拥有

      hline:
      1 : 5 th line (mistical)    
      2 : 5 th line (in legend)
      3 : 4 th line (mistical)    
      4 : 4 th line (in legend)
      5 : 3 th line (mistical)    
      6 : 3 th line (in legend)
      7 : 2 th line (mistical)    
      8 : 2 th line (in legend)
      9 : 1 th line (mistical)    
      10: 1 th line (in legend)
      11: 5 th line (in plot)
      12: 4 th line (in plot)
      13: 3 th line (in plot)
      14: 2 th line (in plot)
      15: 1 th line (in plot)
      

      作为解决方案(周五晚上的拖延症)我做了这个小宝宝:

      解决方案 1:如果您不想重置图例

      检测是否有图例以及绘制了多少行:

      hline = findobj(gcf, 'type', 'line');
      isThereLegend=(~isempty(findobj(gcf,'Type','axes','Tag','legend')))
      
      if(isThereLegend)
          nLines=length(hline)/3
      else
          nLines=length(hline)
      end
      

      为每一行找到正确的句柄并为该行做一些事情(它也适用于相应的图例行)

      for iterLine=1:nLines
          mInd=nLines-iterLine+1
          if(isThereLegend)
              set(hline([(mInd*2-1) (mInd*2) (2*nLines+mInd)]),'LineWidth',iterLine) 
          else
          set(hline(mInd),'LineWidth',iterLine)     
          end
      end
      

      这使得每个i-th 行与width=i 一致,您可以在此处添加自动属性更改;

      解决方案 2:保持简单

      摆脱图例,照顾线条,重置图例。

      legend off
      hline = findobj(gcf, 'type', 'line');
      nLines=length(hline)
      
      for iterLine=1:nLines
          mInd=nLines-iterLine+1
          set(hline(mInd),'LineWidth',iterLine)     
      end
      legend show
      

      这可能不适合必须将图例放置在某个特定位置等情况。

      【讨论】:

        【解决方案4】:

        您也可以在查看器中右键单击该行,然后更改那里的属性。这也改变了相应的“传奇”条目(至少在 2014b 中是这样)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-15
          • 2012-09-10
          • 2012-11-17
          • 1970-01-01
          • 2014-12-15
          • 2022-11-03
          • 1970-01-01
          相关资源
          最近更新 更多