Nickos Raven
Nickos Raven

Reputation: 19

Matlab multiple subplots in gui

I'm currently working on multiple regression in Matlab Gui. I have a variable named A and a matrix named X with many columns, denoted as X1,X2,...Xn. I want to make figure which will have subplots (2 in every row) with plots between A and every X column. The problem is that when the user insert his X matrix, that columns might be 1 ,2 or 10. I think i must make a for loop for X. Is that even possible in subplots? I'm thinking something similar to that. Can someone help me make it work?

     cols = size(X,2);
figure;
for i = 1:cols
    subplot(ceil(cols/2),2,i)
    scatter(A,X(i,:));
end

the output i want must have subplots with the vector A in Y axe with every column of a matrix X. I.e. if the X have 5 columns, I want a figure with 5 subplots.

Upvotes: 0

Views: 259

Answers (1)

Anthony
Anthony

Reputation: 3793

Yes you can. Below is a demonstration.

Quoting from subplot:

subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by p.

Therefore, your a should be total number of columns divided by 2. This, however, could result in non-integers for odd number of columns. You will need to wrap the quotient with ceil.

randomColNum = randi([1,10]);
randomRowNum = randi([10,20]);
A = rand(1,randomRowNum ); % make a random vector to imitate matrix A.
X = rand(randomRowNum, randomColNum ); % make a random matrix to imitate user input X.
cols = size(X,2);
figure;
for i = 1:cols
    subplot(ceil(cols/2),2,i)
    scatter(X(:,i), A);
end

Upvotes: 1

Related Questions