MATLAB: Changing the line properties of a loaded figure?

后端 未结 4 1827
隐瞒了意图╮
隐瞒了意图╮ 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 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.

提交回复
热议问题