multiple graphs in one script

149 次查看(过去 30 天)
basim almutairi
basim almutairi 2021-9-5
回答: Robert U 2021-9-6
Hey all,
I would like to plot several graphs in one script. However, it keeps giving me the last graph one. please see below code
%% 6
x = ([-2*pi:0.1:2*pi]);
f = x-sin(x)
g = 1-x.*cos(x)
plot(x,f,x,g)
title('f(x) & g(x)')
subplot(1,2,1)
plot(x,f)
title('f(x)')
subplot(1,2,2)
plot(x,g)
title('g(x)')
what is the wrong here

回答(2 个)

Wan Ji
Wan Ji 2021-9-5
Modified the code as
%% 6
x = ([-2*pi:0.1:2*pi]);
f = x-sin(x)
g = 1-x.*cos(x)
figure(1);clf;
plot(x,f,x,g)
title('f(x) & g(x)')
figure(2);clf;
subplot(1,2,1)
plot(x,f)
title('f(x)')
subplot(1,2,2)
plot(x,g)
title('g(x)')

Robert U
Robert U 2021-9-6
Hi basim almutairi,
Simplify your life and use handles (start here: graphics-objects): figure handles, axes handles, ...
Handles allow to modify properties even later in code. It makes it easier to debug and to modify plots.
x = (-2*pi:0.1:2*pi);
f = x-sin(x);
g = 1-x.*cos(x);
fh{1} = figure(1);
ah{1} = axes(fh{1});
plot(ah{1},x,f,x,g)
title(ah{1},'f(x) & g(x)')
fh{2} = figure(2);
sh{1} = subplot(1,2,1,'Parent',fh{2});
plot(sh{1},x,f)
title(sh{1},'f(x)')
sh{2} = subplot(1,2,2,'Parent',fh{2});
plot(sh{2},x,g)
title(sh{2},'g(x)')
Kind regards,
Robert

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by