For loop 八度音程使用';对于';语句同时显示两个动画

For loop 八度音程使用';对于';语句同时显示两个动画,for-loop,animation,octave,For Loop,Animation,Octave,我想用“for”语句制作TOF-MS光谱仪(飞行时间质谱)的动画。我能够制作一个脚本,使离子在试管内的飞行具有动画效果。以下是脚本: %Definiranje konstanti e = 1.609e-19; u = 1.6605391e-27; U = 15; L = 1; n = 1; %Ulazni parametri i funkcije m1 = 4; %He m2 = 12; %C m3 = 16; %O T1 = L * sqrt( (m1 * u)/(2 * n * e * 1

我想用“for”语句制作TOF-MS光谱仪(飞行时间质谱)的动画。我能够制作一个脚本,使离子在试管内的飞行具有动画效果。以下是脚本:

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);
  
endfor

%Print slike
print primjer_tof_ms2.png
现在我还想添加一个光谱,它随着离子的飞行和撞击探测器而绘制。光谱是洛伦兹函数的总和,当特定离子撞击探测器时,洛伦兹函数的峰值出现在时间瞬间。我制作了一个脚本,单独为这个情节制作动画:

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

x01 = T1;
x02 = T2;
x03 = T3;
fwhm = 1d-7;
t = 0 : 0.4d-7 : 3d-6;
%Spektar = zbroj Lorentz-ovih funkcija (lf)
lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
     ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
     ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);

for k = 1:length(t)
axes ("position", [0.5, 0.67, 0.2, 0.17]);          
plot (t(1:k), lf(1:k), "linewidth", 2);
axis ([0 3d-6 0 5d6])
legend ("off");
set (gca, "xtick", []);
set (gca, "ytick", []);
ht = title(['t = ' num2str(t(k)) ' s']);
pause (0.001);
delete (ht);
endfor
title(['t = ' num2str(t(k))]);
但是当我尝试组合这两个代码时,在第一次迭代之后,只有光谱图继续动画直到结束,而带有离子的图形停止。我尝试将两个绘图放置在同一个“for”循环中:

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  hold on;                  
                    
  x01 = T1;
  x02 = T2;
  x03 = T3;
  fwhm = 1d-7;
  %Spektar = zbroj Lorentz-ovih funkcija (lf)
  lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);
  
  axes ("position", [0.5, 0.67, 0.2, 0.17]);          
  plot (t(1:k), lf(1:k), "linewidth", 2);
  axis ([0 3d-6 0 5d6])
  legend ("off");
  set (gca, "xtick", []);
  set (gca, "ytick", []);
  ht = title(['t = ' num2str(t(k)) ' s']);
  delete (ht);
  title(['t = ' num2str(t(k))]);                  
                  
  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);

hold off; 
endfor

%Print slike
print primjer_tof_ms2.png
有没有人想到在同一个图形上设置这两组对象的动画

提前谢谢。 致以最良好的祝愿


Igor

您正在使用此行在循环的每次迭代中创建轴

axes ("position", [0.5, 0.67, 0.2, 0.17]); 
这也会将它们设置为当前的。 然后,当你做你的
矩形
时,这些矩形被画在你为光谱创建的小轴上,这些小轴立即被你创建的新轴覆盖。使用
axis equal隐式创建的第一个轴始终保持未使用状态

相反,您应该在循环外部创建图形对象,并在循环内部使用对它们的引用。大多数绘图函数都可以接受轴的句柄作为其第一个参数。也可以在调用绘图函数之前显式设置当前轴。最小exsample为:

h = figure;
ax1 = axes('position',[.1,.5,.4,.4]);
ax2 = axes('position',[.5,.1,.4,.4]);

v = -10; 
wmin = 0; dw=0.1; Wmax = 6*pi();
w = [wmin:dw:Wmax];
t0 = 0; dt = 0.01; T = 10;
for t = t0:dt:T;
    x = w/Wmax .*cos(w+t*v);
    y = w/Wmax .*sin(w+t*v);
    plot(ax1,x,y);%specificaly plot to ax1 (also sets it as current)
    axes(ax2)%set ax2 as current axis 
    plot(w,x,w,y);%plot to the current axis
    pause(0.1)
end

此外,还存在许多警告、意外行为或bug,这些都取决于所选的图形工具包和倍频程版本。我在尝试代码时遇到的一个问题是,
title
对象被
plot
破坏,因此您必须重新创建它,而不是更新它的text属性。我模模糊糊地记得,以前在与八度音阶绘画相关的其他情况下,不得不求助于其他变通方法。因此,了解它的工作原理可能会有所帮助,从长远来看,阅读可能是值得的。我想推荐别的东西,但我只有这些了


因为我已经开始通过尝试你的代码来回答,所以你可以有一个稍微更有效的版本。不过没有保修

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
ax1 = axes();%create axes for particles 
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

ax2 = axes ("position", [0.5, 0.67, 0.2, 0.17]);%create axes for spectrum (also activates them)
axis ([0 3d-6 0 5d6])
legend ("off");
set (ax2, "xtick", []);
set (ax2, "ytick", []);

axes(ax1)%activate axes for particles 
%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  axes(ax1)%activate axes for particles
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  hold on;                  
                    
  x01 = T1;
  x02 = T2;
  x03 = T3;
  fwhm = 1d-7;
  %Spektar = zbroj Lorentz-ovih funkcija (lf)
  lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);
  
  plot (ax2,t(1:k), lf(1:k), "linewidth", 2);%plot on spectrum axes (activates them)

        legend ("off");
        set (ax2, "xtick", []);%these properties are reset by each plot, need to set them in loop
        set (ax2, "ytick", []);
        title(['t = ' num2str(t(k))]);                  
                  
  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);

hold off; 
endfor

亲爱的迪米特里,谢谢你的帮助。我理解问题所在。还有一件事需要指出。绘图不仅重置记号,还重置轴尺寸。因此,它们也需要添加到循环中。而“图例”(在循环之前)必须删除,因为倍频程不知道所指的是哪个绘图。否则,它会起作用。