plotting multiple data on one plot in matlab

To plot multiple data on one plot in Matlab, you can follow these steps:

  1. First, plot the first data using the plot() function.
main.m
plot(x,y)
10 chars
2 lines
  1. After that, use the hold on command to hold the first plot so that subsequent plots can be overlayed on top of it.
main.m
hold on
8 chars
2 lines
  1. Next, plot the next set of data using the plot() function, just like the first one.
main.m
plot(x,z)
10 chars
2 lines
  1. You can continue to add as many additional data sets as you want, always using the plot() function and ensuring that hold on is still active.
main.m
plot(x,w)
10 chars
2 lines
  1. Finally, include a legend on the plot to identify which line corresponds to which data set. This can be achieved using the legend() function, with the input arguments being a cell array of strings that match the order of the plotted data sets.
main.m
legend({'y','z','w'})
22 chars
2 lines

Here is the full example code:

main.m
x = [1:10];
y = x;
z = x.^2;
w = x.^3;

plot(x,y)
hold on
plot(x,z)
plot(x,w)

legend({'y','z','w'})
101 chars
12 lines

This will result in a plot with all the data sets overlaid on top of each other, with a legend identifying which line corresponds to which data set.

gistlibby LogSnag