How can I loop indefinitely, but stop on some condition(s)?

后端 未结 2 1456
刺人心
刺人心 2021-01-12 04:11

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

相关标签:
2条回答
  • 2021-01-12 04:43

    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.

    0 讨论(0)
  • 2021-01-12 05:03
    while (true)
        % block of code here
    end
    
    0 讨论(0)
提交回复
热议问题