Making Multiple Plots Using a For Loop

조회 수: 1 (최근 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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by