Hold a plot other than the most recent

8 views (last 30 days)
If I make a plot and then another plot and later want to add another line to the first plot, is there a way to do this using the hold function or any otehr functions? Basically is there a way to reference the plot you want to add to or will it only had to the most recent plot in the script?

Answers (2)

Benjamin Thompson
Benjamin Thompson on 2 Feb 2022
You can use the figure function to "select" any previously created figure window, as long as you have the figure handle or the figure number. In this example, the first plot is held, and the third call to plot will add a red-colored sinusoid line to the first plot. The second figure window only gets one sinusoid plot in the default color of blue.
>> fig1 = figure;
>> t = 0:10;
>> x1 = 1/2*9.806*t.^2;
>> plot(t, x1)
>> fig2 = figure;
>> x2 = sin(t);
>> plot(t, x2)
>> figure(fig1), hold on
>> plot(t, 2*x2, 'r')
>> hold off

Fangjun Jiang
Fangjun Jiang on 2 Feb 2022

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!