필터 지우기
필터 지우기

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

조회 수: 3 (최근 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
Nicholas Connolly
Nicholas Connolly 2020년 11월 7일
This worked great! Is there a way that I can make different titles for each of the figures?
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