MATLAB。一个脚本单元返回两个数字

标签 matlab plot matlab-figure subplot

我正在尝试创建一个脚本文件,其中包含各种脚本单元格,以 %% 分隔。以下代码返回一个旧图形和一个圆圈。但是,我想清除图形窗口,这样当我执行一个特定脚本时我只能得到一个图形。

% 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)
%%

我得到的是:

enter image description here

如何才能只获得其中一个?

最佳答案

当执行最后一段时,命令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/

相关文章:

matlab - 每 100m 等高线图标签

image - 如何通过矢量化计算图像 block 的方差

Python Igraph 社区集群颜色

python - 如何使用 matplotlib 在两个环之间填充

matlab - 如果 y 方向设置为 'reverse',如何修复 Matlab 中矢量注释头的错误对齐

matlab - MATLAB 图中选定绘图对象的图例

Android C++ Matlab NDK 错误:对::rtNaN 的 undefined reference

python - 在 Windows 平台上创建的 LP 可以在 Linux 平台上运行吗?

matlab - 使用索引矩阵检索具有否定精确索引的矩阵元素?

python - 绘制二维矩阵中特征的存在情况