How do I create many different plots, on different figures, quickly and neatly

1 次查看(过去 30 天)
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)

采纳的回答

Ameer Hamza
Ameer Hamza 2020-11-7
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
  2 个评论
Ameer Hamza
Ameer Hamza 2020-11-8
You can add the titles inside for-loop
X = {x, x2, x3, x4, x5, x6, x7, x8, x9, x10};
Y = {y, y2, y3, y4, y5, y6, y7, y8, y9, y10};
titles = {'title1', 'title2', ...}
for i = 1:numel(X)
figure(i)
plot(X{i}, Y{i})
title(titles{i})
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by