MATLAB: How to create many different plots, on different figures, quickly and neatly

loopplotting

So I need 10 plots, all of them on their own figure. They all have the same x values, but the only thing that is different is the y value. Also I want to add LLSQ line fit to it. Is there a way I can create something neat that will make all those differnt plots without having to do the code below many times? Like can I do a loop where a new y value is put in place?
figure(1)
plot(x,y)
figure(2)
plot(x2,y2)
figure(3)
plot(x3,y3)

Best Answer

The first mistake was creating variable names like this: x, x2, x3. Such variable names make it extremely difficult to write compact code and are discouraged: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. It is better to create a cell array, and then you could easily use for-loop. For example, now you can do something like this
X = {x, x2, x3, x4, x5, x6, x7, x8, x9, x10};
Y = {y, y2, y3, y4, y5, y6, y7, y8, y9, y10};
for i = 1:numel(X)
figure(i)
plot(X{i}, Y{i})
end