2017-09-11 85 views
2

我正在試圖製作一個腳本文件,其中包含各種腳本單元格,由%%分隔。以下代碼返回一箇舊圖和一個圓。但是我想清除數字窗口,所以當我執行一個特定的腳本時,我只能得到一個數字。Matlab。一個腳本單元格返回兩個數字

% 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

如何只得到其中的一個?

回答

1

使用clf (清除圖)從當前圖中刪除所有圖形對象。由於您可能會以隨機順序執行腳本,因此,在每個部分的開頭處使用clf來說明原因。
如果您按照問題中所示的相同順序執行腳本,則可以在子圖之後的部分開始處添加clf

+0

哦,上帝......我一直在按RUN而不是RUN部分......我很抱歉...但是非常感謝代碼,它真的幫助了我很多好友。要遲到了... – Parseval

2

執行最後一段時,由命令subplot(1,2,2)定義的軸仍然是current axes,因此這是添加下一個繪圖的位置。您可以close the previous (i.e. current) figure在那最後一節,以便爲下一個情節創造了一個新的人物和斧頭的開頭:

% Funktionsytor 
close(gcf); 
x=linspace(0,5,50); 
... 

一般情況下,有很多不同的figuresaxes的問題時,最好的做法決定了你應爲每個人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 
+0

我在哪裏添加該命令?我剛剛在命令窗口中輸入它,但它只是克服了數字窗口。然後我跑了腳本,但結果仍然一樣。 – Parseval

+0

@Parseval:你在'%Funktionsytor'後面添加它。 – gnovice

+0

所以現在我只能得到最後一個腳本的情節,但我怎麼才能得到上面的循環腳本的情節? – Parseval

相關問題