Easy Question froma beginner

1 view (last 30 days)
Richard
Richard on 30 Dec 2012
Hi all,
I've registered just to ask this little question. Im sure theres a very simple fix for this but i cant seem to find it.
Im trying to write an m-file, without going into much detail, where im stuck is; well i want matlab to plot like 4 plots (2x2 with subplot) if the input is 4 files. Or or subplot its with 8x10 is the user chooses 80 files.
Is there a function that can do it?
I can only plot a certain number of enteries like this. I would greatly appreciate it if you could help me out.
  1 Comment
Walter Roberson
Walter Roberson on 30 Dec 2012
Suppose the user supplies 79 input files. Should the result be 1 x 79, or should it be 8 x 10 with one empty space, or should it be 9 x 9 with 2 empty spaces? For 80, why is 8 x 10 to be preferred to 9 x 9 with 1 empty space?

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 30 Dec 2012
n=80
t=0:0.1:10;
y=sin(t);
m1=ceil(sqrt(n))
m2=ceil(n/m1)
for k=1:n
subplot(m1,m2,k);
plot(t,y)
end
  2 Comments
Richard
Richard on 30 Dec 2012
Amazing answer thank you!!!
Azzi Abdelmalek
Azzi Abdelmalek on 30 Dec 2012
Image Analyste idea is better, using floor then ceil.
n=80
t=0:0.1:10;
y=sin(t);
m1=floor(sqrt(n))
m2=ceil(n/m1)
for k=1:n
subplot(m1,m2,k);
plot(t,y)
end

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Dec 2012
You're choosing the number of rows and columns incorrectly. Here, run this code and see how to figure out the first two arguments of subplot so that the number of rows and columns is optimized:
for numberOfPlots = 1 : 80
% Get plots in a 3 by 4 ratio of rows to columns.
numberOfRows = floor(sqrt(numberOfPlots));
numberOfColumns = ceil(numberOfPlots/numberOfRows);
fprintf('For %d plots, you should use subplot(%d, %d, n)\n',...
numberOfPlots, numberOfRows, numberOfColumns);
end
For 1 plots, you should use subplot(1, 1, n)
For 2 plots, you should use subplot(1, 2, n)
For 3 plots, you should use subplot(1, 3, n)
For 4 plots, you should use subplot(2, 2, n)
For 5 plots, you should use subplot(2, 3, n)
For 6 plots, you should use subplot(2, 3, n)
For 7 plots, you should use subplot(2, 4, n)
For 8 plots, you should use subplot(2, 4, n)
For 9 plots, you should use subplot(3, 3, n)
For 10 plots, you should use subplot(3, 4, n)
For 11 plots, you should use subplot(3, 4, n)
For 12 plots, you should use subplot(3, 4, n)
For 13 plots, you should use subplot(3, 5, n)
For 14 plots, you should use subplot(3, 5, n)
For 15 plots, you should use subplot(3, 5, n)
For 16 plots, you should use subplot(4, 4, n)
For 17 plots, you should use subplot(4, 5, n)
For 18 plots, you should use subplot(4, 5, n)
For 19 plots, you should use subplot(4, 5, n)
For 20 plots, you should use subplot(4, 5, n)
For 21 plots, you should use subplot(4, 6, n)
For 22 plots, you should use subplot(4, 6, n)
For 23 plots, you should use subplot(4, 6, n)
For 24 plots, you should use subplot(4, 6, n)
For 25 plots, you should use subplot(5, 5, n)
For 26 plots, you should use subplot(5, 6, n)
For 27 plots, you should use subplot(5, 6, n)
For 28 plots, you should use subplot(5, 6, n)
For 29 plots, you should use subplot(5, 6, n)
For 30 plots, you should use subplot(5, 6, n)
For 31 plots, you should use subplot(5, 7, n)
For 32 plots, you should use subplot(5, 7, n)
For 33 plots, you should use subplot(5, 7, n)
For 34 plots, you should use subplot(5, 7, n)
For 35 plots, you should use subplot(5, 7, n)
For 36 plots, you should use subplot(6, 6, n)
For 37 plots, you should use subplot(6, 7, n)
For 38 plots, you should use subplot(6, 7, n)
For 39 plots, you should use subplot(6, 7, n)
For 40 plots, you should use subplot(6, 7, n)
For 41 plots, you should use subplot(6, 7, n)
For 42 plots, you should use subplot(6, 7, n)
For 43 plots, you should use subplot(6, 8, n)
For 44 plots, you should use subplot(6, 8, n)
For 45 plots, you should use subplot(6, 8, n)
For 46 plots, you should use subplot(6, 8, n)
For 47 plots, you should use subplot(6, 8, n)
For 48 plots, you should use subplot(6, 8, n)
For 49 plots, you should use subplot(7, 7, n)
For 50 plots, you should use subplot(7, 8, n)
For 51 plots, you should use subplot(7, 8, n)
For 52 plots, you should use subplot(7, 8, n)
For 53 plots, you should use subplot(7, 8, n)
For 54 plots, you should use subplot(7, 8, n)
For 55 plots, you should use subplot(7, 8, n)
For 56 plots, you should use subplot(7, 8, n)
For 57 plots, you should use subplot(7, 9, n)
For 58 plots, you should use subplot(7, 9, n)
For 59 plots, you should use subplot(7, 9, n)
For 60 plots, you should use subplot(7, 9, n)
For 61 plots, you should use subplot(7, 9, n)
For 62 plots, you should use subplot(7, 9, n)
For 63 plots, you should use subplot(7, 9, n)
For 64 plots, you should use subplot(8, 8, n)
For 65 plots, you should use subplot(8, 9, n)
For 66 plots, you should use subplot(8, 9, n)
For 67 plots, you should use subplot(8, 9, n)
For 68 plots, you should use subplot(8, 9, n)
For 69 plots, you should use subplot(8, 9, n)
For 70 plots, you should use subplot(8, 9, n)
For 71 plots, you should use subplot(8, 9, n)
For 72 plots, you should use subplot(8, 9, n)
For 73 plots, you should use subplot(8, 10, n)
For 74 plots, you should use subplot(8, 10, n)
For 75 plots, you should use subplot(8, 10, n)
For 76 plots, you should use subplot(8, 10, n)
For 77 plots, you should use subplot(8, 10, n)
For 78 plots, you should use subplot(8, 10, n)
For 79 plots, you should use subplot(8, 10, n)
For 80 plots, you should use subplot(8, 10, n)

Community Treasure Hunt

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

Start Hunting!