フィルターのクリア

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 Center および File ExchangeTitle についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by