Matlab – How to plot two figures in MATLAB

matlabplot

I am implementing a clustering algorithm for n data points and I want to plot n data points in a figure before clustering and in another figure after clustering meaning that there should be two figures in same file with same data points.

My code is like:

X = 500*rand([n,2]);
plot(X(:,1), X(:,2), 'r.')                   1

%Some coding section here

After:

symbs = {'r+','g.','bv','m*','ko'};
hold on
for i = 1: length(I)
    plot(X(C==i,1), X(C==i,2), symbs{i})     2
end

I just want to plot (1) in one figure and (2) in another.

Best Solution

Try subplot:

figure;
subplot(1,2,1)
plot(firstdata)
subplot(1,2,2)
plot(seconddata)

This will create two axes areas within the same figure window... from your description, this is my best guess as to what you want.

Edit: From the comments below, here is what you are doing

n=50;
X = 500*rand([n,2]);
subplot(1,2,1); #% <---- add 'subplot' here
plot(X(:,1),X(:,2),'r.')
symbs= {'r+','g.','bv','m*','ko'}; 
subplot(1,2,2); #% <---- add 'subplot' here (with different arguments)
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i})
end

If all you want is a second figure window, instead of doing subplot you can simply say figure in the place where I put the second call to subplot and a new figure window will be created.

figure; #% <--- creates a figure window
n=50;
X = 500*rand([n,2]);
plot(X(:,1),X(:,2),'r.') #% <--- goes in first window


symbs= {'r+','g.','bv','m*','ko'}; 
figure; #% <---- creates another figure window
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i}) #% <--- goes in second window
end