如何无限期循环,但在某些情况下停止? [英] How can I loop indefinitely, but stop on some condition(s)?

查看:164
本文介绍了如何无限期循环,但在某些情况下停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MATLAB进行项目.它包括连续绘制从计算机的串行端口接收到的有关温度的数据.我想无限地做,所以有什么办法可以像在C中那样创建无限循环?

现在if实现为:

while(true)
%comments
end;

如下面的Moore所述,那么有什么方法可以更新这些标志,以便可以根据要求或任何其他操作将其终止?

示例:我正在绘制通过ZigBee进行通信的5个节点的数据,然后,如果我选择在Axis上绘制4个节点的数据,那么在启动无限循环之后还有什么办法,以便可以更改内部使用的数据.遍历MATLAB GUI的输入法或任何标志?

解决方案

对于无限"循环,当满足特定条件时仍然可以轻松地停止它,您可以设置 break 或在循环中遇到 return 命令.


示例:

作为一些您可以停止循环的基于GUI的方式的示例,这里是一个程序,该程序创建一个简单的GUI,该GUI连续递增,并使用while循环每秒显示一次计数器. GUI有两种停止循环的方法:按钮或在图形窗口具有焦点时按 q (使用图的'KeyPressFcn'属性可以在按下键时运行代码).只需将此代码保存在MATLAB路径上某个位置的m文件中,然后运行它以测试示例:

function stop_watch

  hFigure = figure('Position', [200 200 120 70], ...       % Create a figure window
                   'MenuBar', 'none', ...
                   'KeyPressFcn', @stop_keypress);
  hText = uicontrol(hFigure, 'Style', 'text', ...          % Create the counter text
                    'Position', [20 45 80 15], ...
                    'String', '0', ...
                    'HorizontalAlignment', 'center');
  hButton = uicontrol(hFigure, 'Style', 'pushbutton', ...  % Create the button
                      'Position', [20 10 80 25], ...
                      'String', 'Stop', ...
                      'HorizontalAlignment', 'center', ...
                      'Callback', @stop_button);
  counter = -1;
  keepLooping = true;
  while keepLooping       % Loop while keepLooping is true
    counter = counter+1;  % Increment counter
    set(hText, 'String', int2str(counter));  % Update the counter text
    pause(1);             % Pause for 1 second
  end

%---Begin nested functions---

  function stop_keypress(hObject, eventData)
    if strcmp(eventData.Key, 'q')            % If q key is pressed, set
      keepLooping = false;                   %   keepLooping to false
    end
  end

  function stop_button(hObject, eventData)
    keepLooping = false;                     % Set keepLooping to false
  end

end

上面的示例利用了嵌套函数'KeyPressFcn'和按钮回调可以访问和修改stop_watch函数工作区中的keepLooping值.

I am working on a project in MATLAB. It includes continuous plotting of a data about temperature received from the serial port of the computer. I want to do it infinitely so is there any way to create infinite loops like in C?

Now if is implemented as:

while(true)
%comments
end;

as Moore stated below, then is there any way to update the flags so that it could be terminated as per requirement or any other operation?

Example: I am plotting the data for my 5 nodes communicating through ZigBee then if ones I have selected to plot 4 nodes on Axis then is there any way after starting the infinite loop so that I could change the data being used inside the loop through an input method of GUI of MATLAB or any flag?

解决方案

For an "infinite" loop that can still be easily stopped when a certain condition is met, you can set up your while condition to be a logical variable (i.e. flag) that can be updated within your loop:

keepLooping = true;   % A flag that starts as true
while keepLooping
  % Read, process, and plot your data here
  keepLooping = ...;  % Here you would update the value of keepLooping based
                      %   on some condition
end

A while loop can also be terminated if a break or return command is encountered within the loop.


EXAMPLE:

As an example of some of the GUI-based ways you can stop a loop, here is a program that creates a simple GUI that continuously increments and displays a counter once every second using a while loop. The GUI has two ways to stop the loop: a push button or pressing q while the figure window has focus (using the 'KeyPressFcn' property of the figure to run code when a key is pressed). Just save this code in an m-file somewhere on the MATLAB path and run it to test the example:

function stop_watch

  hFigure = figure('Position', [200 200 120 70], ...       % Create a figure window
                   'MenuBar', 'none', ...
                   'KeyPressFcn', @stop_keypress);
  hText = uicontrol(hFigure, 'Style', 'text', ...          % Create the counter text
                    'Position', [20 45 80 15], ...
                    'String', '0', ...
                    'HorizontalAlignment', 'center');
  hButton = uicontrol(hFigure, 'Style', 'pushbutton', ...  % Create the button
                      'Position', [20 10 80 25], ...
                      'String', 'Stop', ...
                      'HorizontalAlignment', 'center', ...
                      'Callback', @stop_button);
  counter = -1;
  keepLooping = true;
  while keepLooping       % Loop while keepLooping is true
    counter = counter+1;  % Increment counter
    set(hText, 'String', int2str(counter));  % Update the counter text
    pause(1);             % Pause for 1 second
  end

%---Begin nested functions---

  function stop_keypress(hObject, eventData)
    if strcmp(eventData.Key, 'q')            % If q key is pressed, set
      keepLooping = false;                   %   keepLooping to false
    end
  end

  function stop_button(hObject, eventData)
    keepLooping = false;                     % Set keepLooping to false
  end

end

The above example makes use of nested functions so that the 'KeyPressFcn' and button callback can access and modify the value of keepLooping in the workspace of the stop_watch function.

这篇关于如何无限期循环,但在某些情况下停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆