如何在 Matlab 中使用更新的输出循环子图?

How to Loop Subplots with Updated Outputs in Matlab?

我想创建一个前端,用户可以通过按 Enter 向前浏览图片。 伪代码

hFig=figure
nFrames=5;
k=1;
while k < nFrames
   u=signal(1*k,100*k,'data.wav'); % 100 length

   subplot(2,2,1);
   plot(u);

   subplot(2,2,2);
   plot(sin(u));

   subplot(2,2,3);
   plot(cos(u));

   subplot(2,2,4);
   plot(tan(u));

   % not necessary but for heading of overal figure
   fprintf('Press Enter for next slice\n');
   str=sprintf('Slice %d', k);
   mtit(hFig, str); 

   k=k+1;
   keyboard

end

function u=signal(a,b,file)
   [fs,smplrt]=audioread(file);
   u=fs(a:b,1);
end

其中

我之前在错误检查中的错误

我之前有一个问题,window 的关闭导致应用程序崩溃。我把它放在这里是因为我在一个答案的评论中提到了一个问题。我现在通过

避免了这个问题
hFig=figure;
n=5;
k=1;
while k<nFrames 

      % for the case, the user closes the window but starts new iteration
      if(not(ishandle(hFig)))
          hFig=figure;
      end

   ...

end

如果用户关闭了先前的图形,它会创建一个新图形。 我之前尝试将 hFig=figure; 放入 while 循环的 if 子句中以避免代码重复,但没有成功。 如果您知道为什么不能在 while 循环的 if 子句中使用句柄 hFig,请告诉我。


如何在 Matlab 中使用更新的输出循环子图?

要停止等待用户输入的脚本,您应该使用 input 而不是 keyboard

实际上 keyboard 使您的脚本进入 debug 模式。它停止脚本的执行(如 breakpoint)允许用户,例如,检查变量的值。

您可以按如下方式修改脚本(修改在脚本末尾,由“更新部分”标识):

hFig=figure
nFrames=5;
k=1;
while k < nFrames
   u=signal(1*k,100*k,'handel.wav'); % 100 length

   subplot(2,2,1);
   plot(u);

   subplot(2,2,2);
   plot(sin(u));

   subplot(2,2,3);
   plot(cos(u));

   subplot(2,2,4);
   plot(tan(u));

   % not necessary but for heading of overal figure
   %
   % UPDATED SECTION
   %
   % Use the string "Press Enter for next slice\n" as the prompt for the
   % call to "input"
   %
   % fprintf('Press Enter for next slice\n');
   % str=sprintf('Slice %f', k);
   % Use %d instead of "%f" to print integer data
   str=sprintf('Slice %d', k);
   mtit(hFig, str); 

   k=k+1;
   % Use "input" instead of "keyboard"
   % keyboard
   input('Press Enter for next slice\n')

end

希望这对您有所帮助。

Qapla'