我正在尝试创建一个脚本文件,其中包含各种脚本单元格,以 %%
分隔。以下代码返回一个旧图形和一个圆圈。但是,我想清除图形窗口,这样当我执行一个特定脚本时我只能得到一个图形。
% Rita tan(x)
x=((-pi/2)+0.01:0.01:(pi/2)-0.01);
y=tan(x);
plot(x,y)
grid on
%%
% Exempel 1
x=linspace(0,8);
y=x.*sin(x);
plot(x,y)
title('f(x)=sin(x)')
%%
% Plot circle
t=linspace(0,2*pi);
x=cos(t); y=sin(t);
subplot(1,2,1)
plot(x,y)
title('Utan axis equal')
subplot(1,2,2)
plot(x,y)
axis equal
title('Med axis equal')
%%
% Funktionsytor
x=linspace(0,5,50);
y=linspace(0,5,50);
[X,Y]= meshgrid(x,y);
F=X.*cos(2*X).*sin(Y);
surf(X,Y,F)
%%
我得到的是:
如何才能只获得其中一个?
最佳答案
当执行最后一段时,命令subplot(1,2,2)
定义的轴仍然是current axes ,这就是您的下一个绘图添加的位置。您可以close the previous (i.e. current) figure在最后一部分的开头,以便为下一个图创建新的图形和轴:
% Funktionsytor
close(gcf);
x=linspace(0,5,50);
...
一般来说,在处理很多不同的figures时或axes ,最佳实践表明您应该 store unique handles对于他们每个人来说。这样您就可以根据需要专门修改/关闭它们。例如,您可以将两个子图绘制在两个单独的图形中,如下所示:
%%
% Plot circle
t = linspace(0, 2*pi);
x = cos(t);
y = sin(t);
hFigure1 = figure(); % Create first figure
plot(x, y); % Plot to axes in first figure
title('Utan axis equal');
hFigure2 = figure(); % Create second figure
plot(x, y); % Plot to axes in second figure
axis equal;
title('Med axis equal');
现在,您可以根据稍后代码中的需要关闭其中一个或两个:
close(hFigure1); % Closes the first figure, second still exists
关于MATLAB。一个脚本单元返回两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46163201/