## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# Hold Everything!

Have you ever tried to overlap more than one plot, only to find that you had to fiddle with a few times before it looked right? Perhaps this is because you aren't take full advantage of the function hold.

### An Example

Suppose I want to plot two sets of data or functions on the same plot. How might I do that? There are several ways. Let's create some data and a function.

t = 0:0.005:1;
f = @(t) sin(2*pi*10*t);
s = f(t);
sn = s + 0.3*randn(size(t));

### Plot the Data and the Function

If I have everything assembled up front, I can plot all the values at once. The function plot is smart enough to cycle through colors for multiple lines.

plot(t,s,t,sn)
legend('signal','signal with noise','Location','SouthEast')

### Try Another Way

Suppose we have the first data values.

plot(t,s)

And then need to add some more values later. Here's a way I can do this.

hold on
plot(t,sn)
legend('signal','signal with noise','Location','SouthEast')
hold off

But the plot is a bit confusing, no? The legend isn't very helpful since each plot has the same color and linestyle. Instead, I can specify the color(s) I want with the plot commands.

plot(t,s)
hold on
plot(t,sn,'g')
legend('signal','signal with noise','Location','SouthEast')
hold off

That green is too bright. To use the one from the first plot, I can find out the correct green color by looking at the defaults.

allcolors = get(0,'defaultAxesColorOrder')
allcolors =
0            0            1
0          0.5            0
1            0            0
0         0.75         0.75
0.75            0         0.75
0.75         0.75            0
0.25         0.25         0.25


You can see that the green is definitely toned down (the color represented by RGB values in the allcolors(2,:).

plot(t,s)
hold on
plot(t,sn,'color',allcolors(2,:))
legend('signal','signal with noise','Location','SouthEast')
hold off

### Why Use Hold?

Why use hold at all if it's giving me headaches? Because sometimes I want to add data to the plot using a different plot function. Here's an example.

fplot(f, [0 1])
hold on
plot(t,sn,'color',allcolors(2,:))
legend('signal','signal with noise','Location','SouthEast')
hold off

### A Better Way

Surely there must be a better way than managing the colors on your own. And there is! The all option to hold magically (quoting from the doc)

holds the plot and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder and ColorOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and linestyles from where the last plot stopped in the list.

Let's try it.

fplot(f, [0 1])
hold all
plot(t,sn)
legend('signal','signal with noise','Location','SouthEast')
hold off

NOTE: I updated this since I didn't use hold off in my examples earlier so old lines were overwriting new ones in the legend. Much better now.

I'd be curious to hear if the behavior of hold all helps you out. Let me know here.

Published with MATLAB® 7.8