Matlab。一个脚本单元格 returns 两个数字
Matlab. One script cell returns two figures
我正在尝试制作一个脚本文件,其中包含各种脚本单元,由 %%
分隔。下面的代码,returns一个老图一个圆圈。但是我想清除数字window所以当我执行一个特定的脚本时我只得到一个数字。
% 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, so that is where your next plot is added. You can close the previous (i.e. current) figure,以便为下一段创建新的图形和轴剧情:
% Funktionsytor
close(gcf);
x=linspace(0,5,50);
...
一般来说,当处理很多不同的东西时figures or axes, best practice dictates that you should store unique handles。这样您就可以根据需要专门 modify/close 它们。例如,您可以将两个子图绘制在两个单独的图形中,如下所示:
%%
% 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
使用clf
(清除图形)删除当前图形中的所有图形对象。由于您可能会以随机顺序执行脚本,因此出于上述原因,请在每个部分的开头使用 clf
。
如果您按照问题中显示的相同顺序执行脚本,那么您只需在子图之后的部分开头添加 clf
。
我正在尝试制作一个脚本文件,其中包含各种脚本单元,由 %%
分隔。下面的代码,returns一个老图一个圆圈。但是我想清除数字window所以当我执行一个特定的脚本时我只得到一个数字。
% 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, so that is where your next plot is added. You can close the previous (i.e. current) figure,以便为下一段创建新的图形和轴剧情:
% Funktionsytor
close(gcf);
x=linspace(0,5,50);
...
一般来说,当处理很多不同的东西时figures or axes, best practice dictates that you should store unique handles。这样您就可以根据需要专门 modify/close 它们。例如,您可以将两个子图绘制在两个单独的图形中,如下所示:
%%
% 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
使用clf
(清除图形)删除当前图形中的所有图形对象。由于您可能会以随机顺序执行脚本,因此出于上述原因,请在每个部分的开头使用 clf
。
如果您按照问题中显示的相同顺序执行脚本,那么您只需在子图之后的部分开头添加 clf
。