multiple instances of the same plot using hold on/off in matlab

To plot multiple instances of the same plot in MATLAB, you can use the "hold on" and "hold off" commands.

Here is an example code:

main.m
% Create data
x = linspace(0, 10);
y1 = sin(x);
y2 = cos(x);

% Plot first instance of the plot
plot(x, y1, 'r-', 'LineWidth', 2);

% Hold on to the previous plot
hold on;

% Plot second instance of the plot
plot(x, y2, 'b--', 'LineWidth', 2);

% Turn hold off
hold off;

% Add titles and labels
title('Multiple instances of the same plot');
xlabel('x');
ylabel('y');
legend('sin(x)', 'cos(x)', 'Location', 'best');
416 chars
23 lines

In this example, we first create data for x, y1, and y2.

Then, we plot the first instance of the plot using the "plot" command.

Next, we use "hold on" to hold on to the previous plot, and plot the second instance of the plot using the "plot" command.

Finally, we use "hold off" to turn off the hold function, and add titles, labels, and legends to the plot.

This will give you multiple instances of the same plot on the same figure.

gistlibby LogSnag