Making Multiple Plots Using a For Loop
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Image Analyst
on 14 Dec 2014
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 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!