问题
For example let's say I have a following matrix (<9x6 double>) with a colheaders(<1x6 cell>).
Matrix =
226.7431 14.7437 14.9417 14.1000 14.5000 66.0590
226.7500 14.6582 14.8250 NaN 14.2000 66.7740
226.7569 14.3590 14.6067 NaN 13.9000 68.4897
226.7639 14.2702 14.5717 13.4000 13.8000 68.2487
226.7708 14.2555 14.6000 NaN 14.0000 NaN
226.7778 14.1605 14.5967 NaN 13.9000 NaN
226.7847 14.0320 14.4567 12.9000 13.6000 68.8272
226.7917 13.8422 14.2733 NaN 13.4000 69.6392
226.7986 13.6585 14.1169 NaN 13.1000 69.8048
I want to plot first column of matrix on x-axis and the rest on y-axis in a matlab figure with subplots (let's say 3 in one figure). Manually I can do something like this a figure and so on.
figure
subplot(3,1,1)
plot(Matrix(:,1),Matrix(:,2),'ro'); grid on; box on; xlabel('A');ylabel('B')
subplot(3,1,2)
plot(Matrix(:,1),Matrix(:,3),'bo'); grid on; box on; xlabel('A');ylabel('C')
subplot(3,1,3)
plot(Matrix(:,1),Matrix(:,4),'go'); grid on; box on; xlabel('A');ylabel('D')
and so on.....
......
......
Now here start a tricky part in which I required help from experts like you guys. I do not want to do manual plotting for my matrix as it consists on 200 columns. So what I want to do a automatic plotting of matrix, so that it plot every column of matrix in subplots. But 200 subplot can not come in one figures, so it start automatically a new figure after subplots limit(let's say 3). Beside I also need to define 'xlabel, ylabel,legend' automatically with a header file 'colheaders'. Is it possible?
回答1:
x = rand(10, 200);
myYLabel = char(64+randi(26, 200, 1));
nrows = 3;
ncols = 2;
for ii = 1:size(x, 2)
if nrows*ncols-mod(-ii, nrows*ncols) == 1
figure;
end
subplot(nrows, ncols, nrows*ncols-mod(-ii, nrows*ncols));
plot(x(:, ii));
ylabel(myYLabel(ii, :));
end
回答2:
It seems to be an easy for-loop task.
I assume you know before how much subplots you want in each figure (let's say 3).
A = yourdatamatrix;
header = [{'A'}, {'B'}, {'C'}, {'D'}, {'E'}, {'F'}];
n = 6 %number of columns
i_figure = 1;
for ii=1:3:n-3
figure(ii)
subplot(3,1,1)
plot(A(:,1),A(:,ii+1),'ro'); grid on; box on; xlabel(header(1));ylabel(header(ii+1))
subplot(3,1,2)
plot(A(:,1),A(:,ii+2),'bo'); grid on; box on; xlabel(header(1));ylabel(header(ii+2))
subplot(3,1,3)
plot(A(:,1),A(:,ii+3),'go'); grid on; box on; xlabel(header(1));ylabel(header(ii+3))
end
I assume also you don't care about your figure number, otherwise just implement another counter. Also be aware that the number of your columns+1 is divisible by 3.
来源:https://stackoverflow.com/questions/19033591/plotting-subplots-in-a-figure-automatically-for-each-column-of-matrix