Making Multiple Plots Using a For Loop

6 ビュー (過去 30 日間)
Bob
Bob 2014 年 12 月 14 日
回答済み: Image Analyst 2014 年 12 月 14 日
I have the matrix A
A=[1 5 7 8; 4 3 2 3; 5 8 7 1]
For all of the plots, the x axis range is x=[1 2 3 4]
I need to have 3 plots total.
From matrix A, I have y1=[1 5 7 8], y2=[4 3 2 3], y3=[5 8 7 1]
I want to plot (x, y1), (x, y2), (x, y3)
How can I increment through the matrix using a for loop so that I can do something like plot(x(i),y(i)) and have all 3 of the plots made in a for loop?
This problem only has 3 plots, but I am going to need to a similar problem with several more plots so it would be really helpful if somebody could show me how to plot this using a for loop.

採用された回答

Image Analyst
Image Analyst 2014 年 12 月 14 日
Try this:
% Create sample data.
x = 1:4;
numberOfRows = 9;
% A=[1 5 7 8; 4 3 2 3; 5 8 7 1]
A = randi(9, numberOfRows, 4)
[rows, columns] = size(A);
% Figure out array size for subplots.
numberOfPlotsInRow = ceil(sqrt(rows))
% Do the plotting.
for row = 1 : rows
% Figure out which place it needs to go into.
subplot(numberOfPlotsInRow, numberOfPlotsInRow, row);
% Do the plot.
plot(x, A(row, :), 'bs-', 'LineWidth', 2);
% Make it fancy.
grid on;
caption = sprintf('Row #%d', row);
title(caption, 'FontSize', 15);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by