How Can I Create More Than One Figure

715 views (last 30 days)
Nathan
Nathan on 1 Mar 2014
Commented: Image Analyst on 14 Nov 2025 at 5:02
I can tell Matlab to make a basic figure such as a plot of 'x' versus 'y', but when I tell Matlab to make more than one figure in the same script, it will delete my first figure when it makes the next one. Do I need to tell Matlab to save each figure after it creates it and before making the next one? or am I missing something very obvious?

Accepted Answer

Image Analyst
Image Analyst on 2 Mar 2014
Edited: Image Analyst on 25 Jan 2025
Something like this to put data into different axes on the same figure window:
% Make plots in a 2 by 2 arrangement on the currently displayed figure window.
subplot(2,2,1); % Upper left
plot(1:10);
subplot(2,2, 2); % Upper right
bar(1:20);
subplot(2,2,3); % Lower left
imshow('cameraman.tif');
subplot(2,2,4); % Lower right
scatter(rand(20,1), rand(20,1));
Or something like this to put data into two separate figure windows:
% Instantiate data.
x = 1:10;
y1 = rand(1, 10);
y2 = sin(x);
% Now plot in a new figure window:
hFig1 = figure('Name', 'This is the first window');
plot(x, y1, 'b.-', 'LineWidth', 3, 'MarkerSize', 30);
grid on;
ylabel('Y1', FontSize=25);
title('Y1 vs. X', 'FontSize', 25)
% Now plot in a totally separate figure window:
hFig2 = figure('Name', 'This is the second window');
plot(x, y2, 'r.-', 'LineWidth', 3, 'MarkerSize', 30);
grid on;
ylabel('Y2', FontSize=25);
title('Y2 vs. X', FontSize=25)
  3 Comments
Dan
Dan on 9 Nov 2025 at 20:04
Edited: Dan on 9 Nov 2025 at 20:04
thank you for answering this 11 years after the fact, i was doing an assignment and had the exact same question as nathan lol
Image Analyst
Image Analyst 13 minutes ago
Or you can do this to put both curves into one axes on a single figure window:
% Instantiate data.
x = 0 : 6 * pi;
y1 = rand(1, numel(x));
y2 = sin(x/3);
% Now plot y1 in a new figure window:
hFig1 = figure('Name', 'This is the only window');
plot(x, y1, 'b.-', 'LineWidth', 3, 'MarkerSize', 30);
grid on;
% Now plot y2 in the same axes in the same figure window:
hold on; % This is the key line to keep the next plot from blowing away the first plot.
plot(x, y2, 'r.-', 'LineWidth', 3, 'MarkerSize', 30);
xlabel('X', FontSize=25);
ylabel('Y1 and Y2', FontSize=25);
title('Y vs. X', FontSize=25)
legend('y1', 'y2', 'Location', 'southwest');

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 1 Mar 2014
Edited: Azzi Abdelmalek on 1 Mar 2014
Use
hold on % many plot in the same figure
Or plot in different figures
figure
plot(x1,y1)
figure
plot(x2,y2)
See also subplot
  2 Comments
Michael
Michael on 25 Jan 2025
I thnk he meant he wanted two different windows

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!