How Can I Create More Than One Figure

728 Ansichten (letzte 30 Tage)
Nathan
Nathan am 1 Mär. 2014
Kommentiert: Image Analyst am 14 Nov. 2025 um 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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Mär. 2014
Bearbeitet: Image Analyst am 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 Kommentare
Dan
Dan am 9 Nov. 2025 um 20:04
Bearbeitet: Dan am 9 Nov. 2025 um 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 am 14 Nov. 2025 um 5:02
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');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 1 Mär. 2014
Bearbeitet: Azzi Abdelmalek am 1 Mär. 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 Kommentare
Michael
Michael am 25 Jan. 2025
I thnk he meant he wanted two different windows
Nathan
Nathan am 25 Jan. 2025

Oh yeah, I think so too

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by