MATLAB: Changing the line properties of a loaded figure?
Asked Answered
K

4

15

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 from the command line? (width, color, marker, etc.)

PD: The first two options according to the information in Defining the Color of Lines for Plotting On this page… only work if you use the plot command. Apparently they are useless if you load the figure.

Kirovograd answered 17/2, 2012 at 14:1 Comment(0)
O
18

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','*') 
Orthodoxy answered 17/2, 2012 at 17:7 Comment(0)
L
2

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)
Limner answered 17/2, 2012 at 16:53 Comment(1)
Thanks for the info. I'll take into consideration. However I find the alternative above, more intuitive.Kirovograd
T
2

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.

Thread answered 4/4, 2014 at 17:35 Comment(0)
T
0

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).

Trstram answered 23/3, 2015 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.