MATLAB: Changing the line properties of a loaded figure?

后端 未结 4 1828
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 19:26

I\'ve got a very simple question, for MATLAB users:

If I load a figure file (.fig) with the load command, is there any way to change the plotted lines properties fro

相关标签:
4条回答
  • 2020-12-28 19:47

    You can get handles for all line objects on current figure with FINDOBJ function:

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

    Then you can change some property for all the line objects:

    set(hline,'LineWidth',3)
    

    or just for some of them :

    set(hline(1),'LineWidth',3) 
    set(hline(2:3),'LineStyle',':') 
    idx = [4 5];
    set(hline(idx),'Marker','*') 
    
    0 讨论(0)
  • 2020-12-28 20:00

    In addition to @yuk answer, if you have a legend drawn as well,

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

    will return N x 3 lines (or more precisely - lines plotted + 2x lines in legend). I will here look only at the case when all the lines that are plotted are also in the legend.

    The sequencing is weird: in case of 5 lines (let us note them 1 to 5) plotted and the legend added, you will have

    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)
    

    As a solution (friday evening procrastination) I made this little baby:

    Solution 1: if you don't want to reset the legend

    Detect if there is a legend and how many lines are plotted:

    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 each line find the right handles and do the stuff for that line (it will apply also to the corresponding legend line)

    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
    

    This makes every i-th line with the width=i and here you can add the automated property changing;

    Solution 2: Keep it simple

    Get rid of the legend, take care of the lines, reset legend.

    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
    

    This might not be suitable for situations when the legend must be placed in some speciffic place etc.

    0 讨论(0)
  • 2020-12-28 20:05

    In order to manipulate objects in a figure, you need to have access to their handles. If you create a figure using plotting functions, these will return handles to you. When you are opening a figure, as is your case, you need to follow a graphic objects tree to find the handle to the specific element you want to manipulate.

    This page has information about the structure of graphics objects.

    The path to the handles you want will depend on your figure, but, as an example, if your figure was created using a simple plot command, this would be one way to change line properties:

    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)
    
    0 讨论(0)
  • 2020-12-28 20:06

    You can also just right click on the line in the viewer, and change the properties there. This also changes the corresponding 'legend' entry (at least it does in 2014b).

    0 讨论(0)
提交回复
热议问题